Skip to content

Commit afc09f6

Browse files
committed
263rd Commit
1 parent 5dd2506 commit afc09f6

File tree

2 files changed

+54
-12
lines changed

2 files changed

+54
-12
lines changed

README.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,15 @@ Problems:
8282
[334]: ./src/page-4/334.%20Increasing%20Triplet%20Subsequence/increasingTriplet.ts
8383
[443]: ./src/page-5/443.%20String%20Compression/compress.ts
8484

85-
| Two Pointers | | | |
86-
| ------------------------------- | ---------------- | ------ | ---- |
87-
| 283. Move Zeroes | [Solution][283] | Easy | 詳解 |
88-
| 392. Is Subsequence | [Solution][392] | Easy | 詳解 |
89-
| 11. Container With Most Water | [Solution][11] | Medium | 詳解 |
90-
| 1679. Max Number of K-Sum Pairs | [Solution][1679] | Medium | 詳解 |
85+
| Two Pointers | | | |
86+
| ------------------------------- | ---------------- | ------ | ----------------------- |
87+
| 283. Move Zeroes | [Solution][283] | Easy | [詳解][283-explanation] |
88+
| 392. Is Subsequence | [Solution][392] | Easy | 詳解 |
89+
| 11. Container With Most Water | [Solution][11] | Medium | 詳解 |
90+
| 1679. Max Number of K-Sum Pairs | [Solution][1679] | Medium | 詳解 |
9191

9292
[283]: ./src/page-3/283.%20Move%20Zeroes/moveZeroes.ts
93+
[283-explanation]: ./src/page-3/283.%20Move%20Zeroes/README.md
9394
[392]: ./src/page-4/392.%20Is%20Subsequence/isSubsequence.ts
9495
[11]: ./src/page-1/11.%20Container%20With%20Most%20Water/maxArea.ts
9596
[1679]: ./src/page-16/1679.%20Max%20Number%20of%20K-Sum%20Pairs/maxOperations.ts
@@ -778,15 +779,16 @@ Problems:
778779
[394]: ./src/page-4/394.%20Decode%20String/decodeString.ts
779780
[739]: ./src/page-7/739.%20Daily%20Temperatures/dailyTemperatures.ts
780781

781-
| Two Pointers | | | |
782-
| ----------------------------- | --------------- | ------ | ---- |
783-
| 11. Container With Most Water | [Solution][11] | Medium | 詳解 |
784-
| 15. 3Sum | Solution | Medium | 詳解 |
785-
| 42. Trapping Rain Water | Solution | Hard | 詳解 |
786-
| 283. Move Zeroes | [Solution][283] | Easy | 詳解 |
782+
| Two Pointers | | | |
783+
| ----------------------------- | --------------- | ------ | ----------------------- |
784+
| 11. Container With Most Water | [Solution][11] | Medium | 詳解 |
785+
| 15. 3Sum | Solution | Medium | 詳解 |
786+
| 42. Trapping Rain Water | Solution | Hard | 詳解 |
787+
| 283. Move Zeroes | [Solution][283] | Easy | [詳解][283-explanation] |
787788

788789
[11]: ./src/page-1/11.%20Container%20With%20Most%20Water/maxArea.ts
789790
[283]: ./src/page-3/283.%20Move%20Zeroes/moveZeroes.ts
791+
[283-explanation]: ./src/page-3/283.%20Move%20Zeroes/README.md
790792

791793
| Trie | | | |
792794
| --------------------------------- | --------------- | ------ | ---- |

src/page-3/283. Move Zeroes/README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 283. 移動零 (Move Zeroes)
2+
3+
給定一個整數陣列 `nums`,將所有 `0` 移至其末端,同時保持非零元素的相對順序。
4+
5+
請注意,你必須在不複製陣列的情況下,在原位對陣列進行操作。
6+
7+
範例 1:
8+
9+
```coffee
10+
輸入: nums = [0,1,0,3,12]
11+
輸出: [1,3,12,0,0]
12+
```
13+
14+
範例 2:
15+
16+
```coffee
17+
輸入: nums = [0]
18+
輸出: [0]
19+
```
20+
21+
## 解題
22+
23+
```ts
24+
export const moveZeroes: MoveZeroes = (nums) => {
25+
// 這將追蹤下一個非零數字應該去的索引
26+
let nonZeroIndex = 0;
27+
28+
// 使用目前指標走訪陣列
29+
for (let i = 0; i < nums.length; i++) {
30+
// 如果目前元素不為 0
31+
if (nums[i] !== 0) {
32+
// 將其與 nonZeroIndex 處的元素交換
33+
[nums[nonZeroIndex], nums[i]] = [nums[i], nums[nonZeroIndex]];
34+
35+
// 將非零指標移到下一個位置
36+
nonZeroIndex += 1;
37+
}
38+
}
39+
};
40+
```

0 commit comments

Comments
 (0)