LeetCode283-移动零

LeetCode283-移动零

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void moveZeroes(int[] nums) {
if (nums.length == 1) return;
int j = 0;
int zeroCount = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i]!=0){
nums[j] = nums[i];
j++;
}else {
zeroCount++;
}
}
while (zeroCount!=0){
nums[nums.length-zeroCount] = 0;
zeroCount--;
}
}