Ninja technique🥷 to ACE DSA Interviews.
class Solution{
public:
void moveZeroes(vector<int> &nums)
{
int ind = 0, n = nums.size();
for (int i = 0; i < n; i++)
if (nums[i] != 0)
nums[ind++] = nums[i];
while (ind < n)
nums[ind++] = 0;
}
};
[1]
no swap required.class Solution{
public:
void moveZeroes(vector<int> &nums)
{
int snowBallSize=0,n=nums.size();
for(int i=0; i < n; i++){
if(nums[i]==0){
snowBallSize++;
}
else{
int temp=nums[i];
nums[i]=0;
nums[i-snowBallSize]=temp;
}
}
}
};
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int n = nums.size();
int i = 0, j = 0;
for(int i = 0;i<n;i++){
if(nums[i]!=0){
swap(nums[i],nums[j]);
j++;
}
}
}
};