Share my c++ solution


  • 0
    W

    a little like partition in quick sort

    class Solution {
    public:
        void moveZeroes(vector<int>& nums) {
            int n=nums.size();
            int i=-1;
            
            // like partition sort, just using 0 as pivot
            for(int j=0; j<n; j++){
                if(nums[j]!=0){
                    swap(nums[++i],nums[j]);
                }
            }
        }
    };

Log in to reply
 

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