From 652a1a227687628abf6d576346c54480c426b0c1 Mon Sep 17 00:00:00 2001 From: Shyam-Chen Date: Mon, 10 Jun 2024 18:10:20 +0800 Subject: [PATCH] 144th Commit --- .../20. Valid Parentheses/is-valid.spec.ts | 11 ------- .../20. Valid Parentheses/isValid.test.ts | 9 ++++++ .../{is-valid.ts => isValid.ts} | 0 .../merge-two-lists.spec.ts | 22 -------------- .../mergeTwoLists.test.ts | 30 +++++++++++++++++++ .../{merge-two-lists.ts => mergeTwoLists.ts} | 0 .../remove-duplicates.spec.ts | 8 ----- .../removeDuplicates.test.ts | 17 +++++++++++ ...move-duplicates.ts => removeDuplicates.ts} | 1 - .../27. Remove Element/remove-element.spec.ts | 8 ----- .../27. Remove Element/removeElement.test.ts | 19 ++++++++++++ .../{remove-element.ts => removeElement.ts} | 0 .../strStr.test.ts | 8 +++++ .../strStr.ts} | 0 .../28. Implement strStr()/str-str.spec.ts | 8 ----- .../search-range.spec.ts | 13 -------- .../searchRange.test.ts | 15 ++++++++++ .../{search-range.ts => searchRange.ts} | 0 18 files changed, 98 insertions(+), 71 deletions(-) delete mode 100644 src/page-1/20. Valid Parentheses/is-valid.spec.ts create mode 100644 src/page-1/20. Valid Parentheses/isValid.test.ts rename src/page-1/20. Valid Parentheses/{is-valid.ts => isValid.ts} (100%) delete mode 100644 src/page-1/21. Merge Two Sorted Lists/merge-two-lists.spec.ts create mode 100644 src/page-1/21. Merge Two Sorted Lists/mergeTwoLists.test.ts rename src/page-1/21. Merge Two Sorted Lists/{merge-two-lists.ts => mergeTwoLists.ts} (100%) delete mode 100644 src/page-1/26. Remove Duplicates from Sorted Array/remove-duplicates.spec.ts create mode 100644 src/page-1/26. Remove Duplicates from Sorted Array/removeDuplicates.test.ts rename src/page-1/26. Remove Duplicates from Sorted Array/{remove-duplicates.ts => removeDuplicates.ts} (99%) delete mode 100644 src/page-1/27. Remove Element/remove-element.spec.ts create mode 100644 src/page-1/27. Remove Element/removeElement.test.ts rename src/page-1/27. Remove Element/{remove-element.ts => removeElement.ts} (100%) create mode 100644 src/page-1/28. Find the Index of the First Occurrence in a String/strStr.test.ts rename src/page-1/{28. Implement strStr()/str-str.ts => 28. Find the Index of the First Occurrence in a String/strStr.ts} (100%) delete mode 100644 src/page-1/28. Implement strStr()/str-str.spec.ts delete mode 100644 src/page-1/34. Find First and Last Position of Element in Sorted Array/search-range.spec.ts create mode 100644 src/page-1/34. Find First and Last Position of Element in Sorted Array/searchRange.test.ts rename src/page-1/34. Find First and Last Position of Element in Sorted Array/{search-range.ts => searchRange.ts} (100%) diff --git a/src/page-1/20. Valid Parentheses/is-valid.spec.ts b/src/page-1/20. Valid Parentheses/is-valid.spec.ts deleted file mode 100644 index df2790c..0000000 --- a/src/page-1/20. Valid Parentheses/is-valid.spec.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { isValid } from './is-valid'; - -describe('20. Valid Parentheses', () => { - it('isValid', () => { - expect(isValid('()')).toEqual(true); - expect(isValid('()[]{}')).toEqual(true); - expect(isValid('(]')).toEqual(false); - expect(isValid('([)]')).toEqual(false); - expect(isValid('{[]}')).toEqual(true); - }); -}); diff --git a/src/page-1/20. Valid Parentheses/isValid.test.ts b/src/page-1/20. Valid Parentheses/isValid.test.ts new file mode 100644 index 0000000..76c4bcf --- /dev/null +++ b/src/page-1/20. Valid Parentheses/isValid.test.ts @@ -0,0 +1,9 @@ +import { isValid } from './isValid'; + +describe('20. Valid Parentheses', () => { + test('isValid', () => { + expect(isValid('()')).toBe(true); + expect(isValid('()[]{}')).toEqual(true); + expect(isValid('(]')).toEqual(false); + }); +}); diff --git a/src/page-1/20. Valid Parentheses/is-valid.ts b/src/page-1/20. Valid Parentheses/isValid.ts similarity index 100% rename from src/page-1/20. Valid Parentheses/is-valid.ts rename to src/page-1/20. Valid Parentheses/isValid.ts diff --git a/src/page-1/21. Merge Two Sorted Lists/merge-two-lists.spec.ts b/src/page-1/21. Merge Two Sorted Lists/merge-two-lists.spec.ts deleted file mode 100644 index 1faf1f4..0000000 --- a/src/page-1/21. Merge Two Sorted Lists/merge-two-lists.spec.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { stringify } from 'flatted'; - -import { generateLinkedList } from '~/utils/linked-list'; - -import { mergeTwoLists } from './merge-two-lists'; - -describe('21. Merge Two Sorted Lists', () => { - it('mergeTwoLists', () => { - // 1->2->4 - const l1 = generateLinkedList([1, 2, 4]); - - // 1->3->4 - const l2 = generateLinkedList([1, 3, 4]); - - const result = mergeTwoLists(l1, l2); - - // 1->1->2->3->4->4 - const spec = generateLinkedList([1, 1, 2, 3, 4, 4]); - - expect(stringify(result)).toEqual(stringify(spec)); - }); -}); diff --git a/src/page-1/21. Merge Two Sorted Lists/mergeTwoLists.test.ts b/src/page-1/21. Merge Two Sorted Lists/mergeTwoLists.test.ts new file mode 100644 index 0000000..a9a8477 --- /dev/null +++ b/src/page-1/21. Merge Two Sorted Lists/mergeTwoLists.test.ts @@ -0,0 +1,30 @@ +import { stringify } from 'flatted'; + +import { generateLinkedList } from '~/utils/linked-list'; + +import { mergeTwoLists } from './mergeTwoLists'; + +describe('21. Merge Two Sorted Lists', () => { + test('mergeTwoLists', () => { + { + const list1 = generateLinkedList([1, 2, 4]); + const list2 = generateLinkedList([1, 3, 4]); + const expected = generateLinkedList([1, 1, 2, 3, 4, 4]); + expect(stringify(mergeTwoLists(list1, list2))).toBe(stringify(expected)); + } + + { + const list1 = generateLinkedList([]); + const list2 = generateLinkedList([]); + const expected = generateLinkedList([]); + expect(stringify(mergeTwoLists(list1, list2))).toBe(stringify(expected)); + } + + { + const list1 = generateLinkedList([]); + const list2 = generateLinkedList([0]); + const expected = generateLinkedList([0]); + expect(stringify(mergeTwoLists(list1, list2))).toBe(stringify(expected)); + } + }); +}); diff --git a/src/page-1/21. Merge Two Sorted Lists/merge-two-lists.ts b/src/page-1/21. Merge Two Sorted Lists/mergeTwoLists.ts similarity index 100% rename from src/page-1/21. Merge Two Sorted Lists/merge-two-lists.ts rename to src/page-1/21. Merge Two Sorted Lists/mergeTwoLists.ts diff --git a/src/page-1/26. Remove Duplicates from Sorted Array/remove-duplicates.spec.ts b/src/page-1/26. Remove Duplicates from Sorted Array/remove-duplicates.spec.ts deleted file mode 100644 index aeae93b..0000000 --- a/src/page-1/26. Remove Duplicates from Sorted Array/remove-duplicates.spec.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { removeDuplicates } from './remove-duplicates'; - -describe('26. Remove Duplicates from Sorted Array', () => { - it('removeDuplicates', () => { - expect(removeDuplicates([1, 1, 2])).toEqual(2); - expect(removeDuplicates([0, 0, 1, 1, 1, 2, 2, 3, 3, 4])).toEqual(5); - }); -}); diff --git a/src/page-1/26. Remove Duplicates from Sorted Array/removeDuplicates.test.ts b/src/page-1/26. Remove Duplicates from Sorted Array/removeDuplicates.test.ts new file mode 100644 index 0000000..5b3e9ab --- /dev/null +++ b/src/page-1/26. Remove Duplicates from Sorted Array/removeDuplicates.test.ts @@ -0,0 +1,17 @@ +import { removeDuplicates } from './removeDuplicates'; + +describe('26. Remove Duplicates from Sorted Array', () => { + test('removeDuplicates', () => { + { + const nums = [1, 1, 2]; + expect(removeDuplicates(nums)).toBe(2); + expect(nums).toStrictEqual([1, 2]); + } + + { + const nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]; + expect(removeDuplicates(nums)).toBe(5); + expect(nums).toStrictEqual([0, 1, 2, 3, 4]); + } + }); +}); diff --git a/src/page-1/26. Remove Duplicates from Sorted Array/remove-duplicates.ts b/src/page-1/26. Remove Duplicates from Sorted Array/removeDuplicates.ts similarity index 99% rename from src/page-1/26. Remove Duplicates from Sorted Array/remove-duplicates.ts rename to src/page-1/26. Remove Duplicates from Sorted Array/removeDuplicates.ts index e6b6e3a..7cf6e2d 100644 --- a/src/page-1/26. Remove Duplicates from Sorted Array/remove-duplicates.ts +++ b/src/page-1/26. Remove Duplicates from Sorted Array/removeDuplicates.ts @@ -7,6 +7,5 @@ export const removeDuplicates: RemoveDuplicates = (nums) => { const result = Array.from(new Set(nums)); nums.length = 0; nums.push(...result); - return new Set(nums).size; }; diff --git a/src/page-1/27. Remove Element/remove-element.spec.ts b/src/page-1/27. Remove Element/remove-element.spec.ts deleted file mode 100644 index 4ea934b..0000000 --- a/src/page-1/27. Remove Element/remove-element.spec.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { removeElement } from './remove-element'; - -describe('27. Remove Element', () => { - it('removeElement', () => { - expect(removeElement([3, 2, 2, 3], 3)).toEqual(2); - expect(removeElement([0, 1, 2, 2, 3, 0, 4, 2], 2)).toEqual(5); - }); -}); diff --git a/src/page-1/27. Remove Element/removeElement.test.ts b/src/page-1/27. Remove Element/removeElement.test.ts new file mode 100644 index 0000000..7482c26 --- /dev/null +++ b/src/page-1/27. Remove Element/removeElement.test.ts @@ -0,0 +1,19 @@ +import { removeElement } from './removeElement'; + +describe('27. Remove Element', () => { + test('removeElement', () => { + { + const nums = [3, 2, 2, 3]; + const expected = [2, 2]; + expect(removeElement(nums, 3)).toBe(expected.length); + expect(nums.sort()).toStrictEqual(expected.sort()); + } + + { + const nums = [0, 1, 2, 2, 3, 0, 4, 2]; + const expected = [0, 1, 4, 0, 3]; + expect(removeElement(nums, 2)).toBe(expected.length); + expect(nums.sort()).toStrictEqual(expected.sort()); + } + }); +}); diff --git a/src/page-1/27. Remove Element/remove-element.ts b/src/page-1/27. Remove Element/removeElement.ts similarity index 100% rename from src/page-1/27. Remove Element/remove-element.ts rename to src/page-1/27. Remove Element/removeElement.ts diff --git a/src/page-1/28. Find the Index of the First Occurrence in a String/strStr.test.ts b/src/page-1/28. Find the Index of the First Occurrence in a String/strStr.test.ts new file mode 100644 index 0000000..7b6f358 --- /dev/null +++ b/src/page-1/28. Find the Index of the First Occurrence in a String/strStr.test.ts @@ -0,0 +1,8 @@ +import { strStr } from './strStr'; + +describe('28. Find the Index of the First Occurrence in a String', () => { + test('strStr', () => { + expect(strStr('sadbutsad', 'sad')).toBe(0); + expect(strStr('leetcode', 'leeto')).toBe(-1); + }); +}); diff --git a/src/page-1/28. Implement strStr()/str-str.ts b/src/page-1/28. Find the Index of the First Occurrence in a String/strStr.ts similarity index 100% rename from src/page-1/28. Implement strStr()/str-str.ts rename to src/page-1/28. Find the Index of the First Occurrence in a String/strStr.ts diff --git a/src/page-1/28. Implement strStr()/str-str.spec.ts b/src/page-1/28. Implement strStr()/str-str.spec.ts deleted file mode 100644 index 163b54e..0000000 --- a/src/page-1/28. Implement strStr()/str-str.spec.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { strStr } from './str-str'; - -describe('28. Implement strStr()', () => { - it('strStr', () => { - expect(strStr('hello', 'll')).toEqual(2); - expect(strStr('aaaaa', 'bba')).toEqual(-1); - }); -}); diff --git a/src/page-1/34. Find First and Last Position of Element in Sorted Array/search-range.spec.ts b/src/page-1/34. Find First and Last Position of Element in Sorted Array/search-range.spec.ts deleted file mode 100644 index f0ae140..0000000 --- a/src/page-1/34. Find First and Last Position of Element in Sorted Array/search-range.spec.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { searchRange, searchRange2 } from './search-range'; - -describe('34. Find First and Last Position of Element in Sorted Array', () => { - it('searchRange', () => { - expect(searchRange([5, 7, 7, 8, 8, 10], 8)).toEqual([3, 4]); - expect(searchRange([5, 7, 7, 8, 8, 10], 6)).toEqual([-1, -1]); - }); - - it('searchRange2', () => { - expect(searchRange2([5, 7, 7, 8, 8, 10], 8)).toEqual([3, 4]); - expect(searchRange2([5, 7, 7, 8, 8, 10], 6)).toEqual([-1, -1]); - }); -}); diff --git a/src/page-1/34. Find First and Last Position of Element in Sorted Array/searchRange.test.ts b/src/page-1/34. Find First and Last Position of Element in Sorted Array/searchRange.test.ts new file mode 100644 index 0000000..314a382 --- /dev/null +++ b/src/page-1/34. Find First and Last Position of Element in Sorted Array/searchRange.test.ts @@ -0,0 +1,15 @@ +import { searchRange, searchRange2 } from './searchRange'; + +describe('34. Find First and Last Position of Element in Sorted Array', () => { + it('searchRange', () => { + expect(searchRange([5, 7, 7, 8, 8, 10], 8)).toStrictEqual([3, 4]); + expect(searchRange([5, 7, 7, 8, 8, 10], 6)).toStrictEqual([-1, -1]); + expect(searchRange([], 0)).toStrictEqual([-1, -1]); + }); + + it('searchRange2', () => { + expect(searchRange2([5, 7, 7, 8, 8, 10], 8)).toStrictEqual([3, 4]); + expect(searchRange2([5, 7, 7, 8, 8, 10], 6)).toStrictEqual([-1, -1]); + expect(searchRange2([], 0)).toStrictEqual([-1, -1]); + }); +}); diff --git a/src/page-1/34. Find First and Last Position of Element in Sorted Array/search-range.ts b/src/page-1/34. Find First and Last Position of Element in Sorted Array/searchRange.ts similarity index 100% rename from src/page-1/34. Find First and Last Position of Element in Sorted Array/search-range.ts rename to src/page-1/34. Find First and Last Position of Element in Sorted Array/searchRange.ts