原题链接
leet hot 100-4
283. 移动零
思路
遍历数组 将非0数字 移动到数组前端 数字0就会被移动到数组末端时间复杂度O(n) 空间复杂度(n)
代码
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int start = 0;
int index = 0;
while(index != nums.size())
{
if(nums[index] != 0)
{
swap(nums[start],nums[index]);
start++;
}
index++;
}
}
};