Skip to content

Commit b548873

Browse files
authored
Create Solution.java
1 parent c39ce07 commit b548873

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public void duplicateZeros(int[] arr) {
3+
int n = arr.length;
4+
int i = 0, j = 0;
5+
while (j < n) {
6+
if (arr[i] == 0) ++j;
7+
++i;
8+
++j;
9+
}
10+
--i; // i 回到最后一次合法的位置
11+
--j; // j 同理,但 j 仍可能等于 n(例如输入 [0])
12+
while (i >= 0) {
13+
if (j < n) arr[j] = arr[i];
14+
if (arr[i] == 0) arr[--j] = arr[i];
15+
--i;
16+
--j;
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)