C++, without extra space , without pointer


  • 0
    Q
    class Solution {
     public:
    void moveZeroes(vector<int>& nums) {
        if(nums.size()==0)
           return ;
        int j=-1;
        for(int i=0;i<nums.size();i++)
        {
            if(nums[i]!=0)
            {
                j++;
                nums[j]=nums[i];
            }
        }
        j++;
        for(;j<nums.size();j++)
          nums[j]=0;
      }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.