移动零
链接
题目
题解
异地变本地,首先我们可以将所有的非0元素移动到开头即可,使用双指针,原理如下图
代码
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int l = -1, r = 0;
while (r < nums.size())
if (nums[r] != 0) swap(nums[++l], nums[r++]);
else r++;
}
};