标签:int 就会 i++ lan block move 指示器 tin ++
位置指示器法
我们将cnt看作位置指示器,易于发现规律:某个不为0的元素前面有几个0(cnt),他就会向前移动cnt个位置。
class Solution {
public void moveZeroes(int[] nums) {
int cnt = 0;
for(int i = 0; i < nums.length; i++) {
if(nums[i] == 0) {
cnt++;
continue;
}
int t = nums[i];
nums[i] = nums[i - cnt];
nums[i - cnt] = t;
}
}
}
标签:int 就会 i++ lan block move 指示器 tin ++
原文地址:https://www.cnblogs.com/fromneptune/p/13232518.html