0324.cpp (557B)
1 class Solution { 2 3 public: 4 void wiggleSort(vector<int> &nums) const { 5 const int n = size(nums); 6 7 const auto midptr = begin(nums) + n / 2; 8 nth_element(begin(nums), midptr, end(nums)); 9 const int mid = *midptr; 10 11 #define map(i) nums[(2 * (i) + 1) % (n | 1)] 12 int i = 0, j = 0, k = n - 1; 13 while (j <= k) { 14 if (map(j) > mid) 15 swap(map(i++), map(j++)); 16 else if (map(j) < mid) 17 swap(map(j), map(k--)); 18 else 19 j++; 20 } 21 } 22 };