Sharing my 36ms C++ solution


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

Log in to reply
 

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