wiggle sort

code

class Solution {
public:
    /**
     * @param nums a list of integer
     * @return void
     */  
    void wiggleSort(vector<int>& nums) {
        // Write your code here
        for(int i = 1; i<nums.size(); i++)
        {
            if(i%2 == 1 && nums[i] < nums[i-1])
            {
                swap(nums, i, i-1);
            }

            if(i%2 == 0 && nums[i] > nums[i-1])
            {
                swap(nums, i, i-1);
            }

        }
    }

    void swap(vector<int>& nums, int i, int j)
    {
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
};

results matching ""

    No results matching ""