324_WiggleSortII - a920604a/leetcode GitHub Wiki
class Solution {
public:
void wiggleSort(vector<int>& nums) {
sort(nums.begin(), nums.end());
int n = nums.size();
int l = (n-1)/2, r = n-1;
vector<int> ret(n,0);
for(int i=0;i<n;++i){
if(i%2==0) ret[i] = nums[l--];
else ret[i] = nums[r--];
}
nums = ret;
}
};
- time complexity
O(nlogn)
- space complexity
O(n)