Skip to content

Commit

Permalink
233rd Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyam-Chen committed Oct 21, 2024
1 parent 04bfc4e commit 98fe0d4
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 3 deletions.
5 changes: 5 additions & 0 deletions algorithms/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"imports": {
"@std/data-structures": "jsr:@std/data-structures@^1.0.4"
}
}
16 changes: 16 additions & 0 deletions algorithms/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 32 additions & 2 deletions algorithms/heap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,22 @@ import { BinaryHeap, ascend } from '@std/data-structures';

const minHeap = new BinaryHeap<number>(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)
Expand All @@ -55,5 +70,20 @@ import { BinaryHeap, descend } from '@std/data-structures';

const maxHeap = new BinaryHeap<number>(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
```
20 changes: 20 additions & 0 deletions algorithms/heap/maxHeap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { BinaryHeap, descend } from '@std/data-structures';

const maxHeap = new BinaryHeap<number>(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
20 changes: 20 additions & 0 deletions algorithms/heap/minHeap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { BinaryHeap, ascend } from '@std/data-structures';

const minHeap = new BinaryHeap<number>(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
12 changes: 11 additions & 1 deletion src/page-3/283. Move Zeroes/moveZeroes.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { moveZeroes } from './moveZeroes';
import { moveZeroes, moveZeroes2 } from './moveZeroes';

describe('283. Move Zeroes', () => {
test('moveZeroes', () => {
Expand All @@ -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]);
});
});
20 changes: 20 additions & 0 deletions src/page-3/283. Move Zeroes/moveZeroes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
};

0 comments on commit 98fe0d4

Please sign in to comment.