Partition method c++


  • 3
    X
    //move zeroes: partition method: O(n)
    
    class Solution {
    public:
        void moveZeroes(vector<int>& nums) {
            if (nums.size() == 0 || nums.size() == 1) return;
    
            int nonZeroPartitions = 0;
            for (int i = 0; i < nums.size(); i++) {
                if (nums[i] != 0) {
                    int tmp = nums[nonZeroPartitions];
                    nums[nonZeroPartitions] = nums[i];
                    nums[i] = tmp;
                    nonZeroPartitions++;
                }
            }
        }
    };

Log in to reply
 

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