One pass inplace C++ 20 ms, seeking better solution


  • 0
    N
    class Solution {
    public:
        void swap(int * a , int *b){
            int tmp = *a;
            *a =*b ;
            *b = tmp;
        }
        void moveZeroes(vector<int>& nums) {
            int j = 0;
            for(int i = 0; i < nums.size(); i++){
                if(nums[i]!=0) {
                    swap(&nums[i], &nums[j]);
                    j++;
                }
            }
        }
    };
    

    How to do this use even less operations?


Log in to reply
 

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