One pass in-place cpp solution


  • 0
    P
    class Solution {
    public:
        void wiggleSort(vector<int>& nums) {
            int n = nums.size();
            if(n<=1) return;
            int flip = 1;
            for(int i=0;i<n-1;i++){
                if(flip && nums[i]>nums[i+1]) swap(nums[i],nums[i+1]);
                else if(!flip && nums[i]<nums[i+1]) swap(nums[i],nums[i+1]);
                flip = 1-flip;
            }
            return;
        }
    };
    

Log in to reply
 

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