移动零
题目链接:移动零
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
请注意 ,必须在不复制数组的情况下原地对数组进行操作。
方法:哈希表
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int n = nums.size(),l = 0,r = 0;
while (r < n) {
if (nums[r]) {
swap(nums[l],nums[r]);
l++;
}
r++;
}
}
};