Skip to content

Latest commit

 

History

History

280

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]....

Example:

Input: nums = [3,5,2,1,6,4]
Output: One possible answer is [3,5,1,6,2,4]

Companies:
Microsoft, Google, Amazon

Related Topics:
Array, Sort

Similar Questions:

Solution 1.

// OJ: https://leetcode.com/problems/wiggle-sort/
// Author: github.com/lzl124631x
// Time: O(NlogN)
// Space: O(1)
class Solution {
public:
    void wiggleSort(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        for (int i = 1; i + 1 < nums.size(); i += 2) {
            swap(nums[i], nums[i + 1]);
        }
    }
};