题目描述
思路
由于之前已经做过27题了,所以这题的思路也就不言而喻了。首先按照27题的做法,将值不为0的提前,然后将数组新长度之后的值全赋值为0即可。
class Solution {
public void moveZeroes(int[] nums) {
int left = 0;
for (int right = 0; right < nums.length; right++) {
if (nums[right] != 0) {
nums[left] = nums[right];
left++;
}
}
for (int right = left; right < nums.length; right++) {
nums[right] = 0;
}
}
}