给你一个没有排序的数组,请将原数组就地重新排列满足如下性质
nums[0] <= nums[1] >= nums[2] <= nums[3]….
注意事项:
请就地排序数组,也就是不需要额外数组
样例:
给出数组为 nums = [3, 5, 2, 1, 6, 4] 一种输出方案为 [1, 6, 2, 5, 3, 4]
#ifndef C508_H
#define C508_H
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
/*
* @param nums: A list of integers
* @return: nothing
*/
void wiggleSort(vector<int> &nums) {
// write your code here
if (nums.empty())
return;
int len = nums.size();
sort(nums.begin(), nums.end());
int l = 0, r = len - 1;
while (l < r)
{
if (l % 2 == 1 && r % 2 == 0)
{
swap(nums[l], nums[r]);
++l;
--r;
}
else if (l % 2 == 0 && r % 2 == 0)
++l;
else if (l % 2 == 1 && r % 2 == 1)
--r;
else
{
++l;
--r;
}
}
}
};
#endif