classSolution{public:voidmoveZeroes(vector<int>&nums){intn=nums.size();// Iterate to left to rightfor(inti=0;i<n;i++){// If the number at `i` is zero, so we need a non-zero// number to swap this zero with non-zeroif(nums[i]==0){// We find the non-zero element from after the `i` till the end// And swap itfor(intj=i+1;j<n;j++){// If this `j` is not zero, then we can swap itif(nums[j]!=0){swap(nums[i],nums[j]);break;// Break the loop for next position}}}}}};
classSolution{public:voidmoveZeroes(vector<int>&nums){intn=nums.size();// This is correct spots of the non-zero elements// It will start from index 0intwritePointer=0;// We will iterate in the array using `readPointer`for(intreadPointer=0;readPointer<n;readPointer++){// If the `readPointer` element is non-zeroif(nums[readPointer]!=0){// We swap this element with our `writePointer` spotswap(nums[writePointer],nums[readPointer]);// And move `writePointer` one step aheadwritePointer++;}}}};
Warning
LINK
You are about to visit a link which has been flagged with the above content warnings. Do you wish to continue?