classSolution{public:voidmoveZeroes(vector<int>& nums){int put_pos =0,size = nums.size();for(int i =0;i < size;++i)if(nums[i])swap(nums[put_pos++],nums[i]);return;}};
代码实现(四刷自解 DAY 299 C++)
classSolution{public:voidmoveZeroes(vector<int>& nums){int index =0;for(int i =0; i < nums.size();++i){if(!nums[i])continue;swap(nums[i], nums[index++]);}}};
代码实现(五刷自解 DAY 10 Golang)
funcmoveZeroes(nums []int){for pos, i :=0,0; i <len(nums); i++{if nums[i]==0{continue}
nums[pos], nums[i]= nums[i], nums[pos]
pos++}}