leetcodeSolution to some Leetcode problems written in C++ |
git clone git://git.dimitrijedobrota.com/leetcode.git |
Log | Files | Refs | README | LICENSE |
0324.cpp (557B)
0 class Solution {
2 public:
3 void wiggleSort(vector<int> &nums) const {
4 const int n = size(nums);
6 const auto midptr = begin(nums) + n / 2;
7 nth_element(begin(nums), midptr, end(nums));
8 const int mid = *midptr;
10 #define map(i) nums[(2 * (i) + 1) % (n | 1)]
11 int i = 0, j = 0, k = n - 1;
12 while (j <= k) {
13 if (map(j) > mid)
14 swap(map(i++), map(j++));
15 else if (map(j) < mid)
16 swap(map(j), map(k--));
17 else
18 j++;
19 }
20 }
21 };