diff --git a/algorithms/deno.json b/algorithms/deno.json new file mode 100644 index 0000000..92ed56b --- /dev/null +++ b/algorithms/deno.json @@ -0,0 +1,5 @@ +{ + "imports": { + "@std/data-structures": "jsr:@std/data-structures@^1.0.4" + } +} diff --git a/algorithms/deno.lock b/algorithms/deno.lock new file mode 100644 index 0000000..92ed06e --- /dev/null +++ b/algorithms/deno.lock @@ -0,0 +1,16 @@ +{ + "version": "4", + "specifiers": { + "jsr:@std/data-structures@^1.0.4": "1.0.4" + }, + "jsr": { + "@std/data-structures@1.0.4": { + "integrity": "fa0e20c11eb9ba673417450915c750a0001405a784e2a4e0c3725031681684a0" + } + }, + "workspace": { + "dependencies": [ + "jsr:@std/data-structures@^1.0.4" + ] + } +} diff --git a/algorithms/heap/README.md b/algorithms/heap/README.md index 85ff4e2..16b9862 100644 --- a/algorithms/heap/README.md +++ b/algorithms/heap/README.md @@ -29,7 +29,22 @@ import { BinaryHeap, ascend } from '@std/data-structures'; const minHeap = new BinaryHeap(ascend); -minHeap.push(1, 3, 5, 6, 8, 9, 15); +// 元素入堆積 +minHeap.push(1, 3, 6, 5, 9, 8, 15); + +// 取得堆積頂元素 +console.log(minHeap.peek()); // 1 + +// 頂元素出堆積 +console.log(minHeap.pop()); // 1 +console.log(minHeap.pop()); // 3 +console.log(minHeap.pop()); // 5 + +// 取得堆積大小 +console.log(minHeap.length); // 4 + +// 判斷堆積是否為空 +console.log(minHeap.isEmpty()); // false ``` ## 大頂堆積 (Max Heap) @@ -55,5 +70,20 @@ import { BinaryHeap, descend } from '@std/data-structures'; const maxHeap = new BinaryHeap(descend); -maxHeap.push(1, 3, 5, 6, 8, 9, 15); +// 元素入堆積 +maxHeap.push(1, 3, 6, 5, 9, 8, 15); + +// 取得堆積頂元素 +console.log(maxHeap.peek()); // 15 + +// 頂元素出堆積 +console.log(maxHeap.pop()); // 15 +console.log(maxHeap.pop()); // 9 +console.log(maxHeap.pop()); // 8 + +// 取得堆積大小 +console.log(maxHeap.length); // 4 + +// 判斷堆積是否為空 +console.log(maxHeap.isEmpty()); // false ``` diff --git a/algorithms/heap/maxHeap.ts b/algorithms/heap/maxHeap.ts new file mode 100644 index 0000000..d7fa0d9 --- /dev/null +++ b/algorithms/heap/maxHeap.ts @@ -0,0 +1,20 @@ +import { BinaryHeap, descend } from '@std/data-structures'; + +const maxHeap = new BinaryHeap(descend); + +// 元素入堆積 +maxHeap.push(1, 3, 6, 5, 9, 8, 15); + +// 取得堆積頂元素 +console.log(maxHeap.peek()); // 15 + +// 頂元素出堆積 +console.log(maxHeap.pop()); // 15 +console.log(maxHeap.pop()); // 9 +console.log(maxHeap.pop()); // 8 + +// 取得堆積大小 +console.log(maxHeap.length); // 4 + +// 判斷堆積是否為空 +console.log(maxHeap.isEmpty()); // false diff --git a/algorithms/heap/minHeap.ts b/algorithms/heap/minHeap.ts new file mode 100644 index 0000000..baaaa61 --- /dev/null +++ b/algorithms/heap/minHeap.ts @@ -0,0 +1,20 @@ +import { BinaryHeap, ascend } from '@std/data-structures'; + +const minHeap = new BinaryHeap(ascend); + +// 元素入堆積 +minHeap.push(1, 3, 6, 5, 9, 8, 15); + +// 取得堆積頂元素 +console.log(minHeap.peek()); // 1 + +// 頂元素出堆積 +console.log(minHeap.pop()); // 1 +console.log(minHeap.pop()); // 3 +console.log(minHeap.pop()); // 5 + +// 取得堆積大小 +console.log(minHeap.length); // 4 + +// 判斷堆積是否為空 +console.log(minHeap.isEmpty()); // false diff --git a/src/page-3/283. Move Zeroes/moveZeroes.test.ts b/src/page-3/283. Move Zeroes/moveZeroes.test.ts index 5752236..ece7582 100644 --- a/src/page-3/283. Move Zeroes/moveZeroes.test.ts +++ b/src/page-3/283. Move Zeroes/moveZeroes.test.ts @@ -1,4 +1,4 @@ -import { moveZeroes } from './moveZeroes'; +import { moveZeroes, moveZeroes2 } from './moveZeroes'; describe('283. Move Zeroes', () => { test('moveZeroes', () => { @@ -10,4 +10,14 @@ describe('283. Move Zeroes', () => { moveZeroes(nums2); expect(nums2).toStrictEqual([0]); }); + + test('moveZeroes2', () => { + const nums1 = [0, 1, 0, 3, 12]; + moveZeroes2(nums1); + expect(nums1).toStrictEqual([1, 3, 12, 0, 0]); + + const nums2 = [0]; + moveZeroes2(nums2); + expect(nums2).toStrictEqual([0]); + }); }); diff --git a/src/page-3/283. Move Zeroes/moveZeroes.ts b/src/page-3/283. Move Zeroes/moveZeroes.ts index ef067f8..b57327b 100644 --- a/src/page-3/283. Move Zeroes/moveZeroes.ts +++ b/src/page-3/283. Move Zeroes/moveZeroes.ts @@ -15,3 +15,23 @@ export const moveZeroes: MoveZeroes = (nums) => { } } }; + +/** + * Accepted + */ +export const moveZeroes2: MoveZeroes = (nums) => { + let left = 0; // Left pointer, points to the next position where a non-zero element should be placed + let right = 0; // Right pointer, traverses the entire array + + while (right < nums.length) { + // If the number at the right pointer is not zero + if (nums[right] !== 0) { + // Swap the numbers at the left and right pointers + [nums[left], nums[right]] = [nums[right], nums[left]]; + + left += 1; // Move the left pointer to the next position + } + + right += 1; // Continue moving the right pointer to the right + } +};