Accepted O(n) C++ code


  • 0
    C
        void moveZeroes(vector<int>& nums) {
            int count = 0, i = 0;
            
            vector <int> arr;
            for(int i = 0; i < nums.size(); ++i) {
                if(nums[i] == 0){
                    ++count;
                } else {
                    arr.push_back(nums[i]);
                }
            }
            while(count > 0){
                arr.push_back(0);
                --count;
            }
            swap(nums, arr);
        }

Log in to reply
 

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