给定一些数,要把他们重新排列成满足:
< > < > < …关系的序列
思路:
从小到大排序,然后将奇数位,从左向右从大到小放置;将偶数位从左向右,从大到小放置,即可得到满足的结果序列
Code:
class Solution { public: void wiggleSort(vector<int>& nums) { sort(nums.begin(), nums.end()); int n = nums.size(); vector<int> v(n); int r = n - 1,l = 1; for(;l < n;l += 2) { v[l] = nums[r]; r --; } for(l = 0;l < n;l += 2) { v[l] = nums[r]; r --; } nums = v; } };