C++_Swap_Accepted_39ms_81.15%


  • 1

    It looks like bubble sort. Very easy.

     class Solution {
    public:
    void wiggleSort(vector<int>& nums) {
        while(true){
            int less = 1;
            int k = 0;
            for(int i = 1; i < nums.size(); ++i){
                if(less){
                    if(nums[i-1] > nums[i]){swap(nums[i-1], nums[i]); k = 1;}
                }else{
                    if(nums[i-1] < nums[i]){swap(nums[i-1], nums[i]); k = 1;}
                }
                less = less ^ 1;
            }
            if(k == 0) return;
        }
    }
    };
    

    0_1476748234165_5DA05B4D-50D5-4384-82DE-4B15C32F15A6.png


Log in to reply
 

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