-
-
Notifications
You must be signed in to change notification settings - Fork 299
/
280.cpp
26 lines (26 loc) · 929 Bytes
/
280.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
__________________________________________________________________________________________________
// Time Complexity O(nlgn)
class Solution {
public:
void wiggleSort(vector<int> &nums) {
sort(nums.begin(), nums.end());
if (nums.size() <= 2) return;
for (int i = 2; i < nums.size(); i += 2) {
swap(nums[i], nums[i - 1]);
}
}
};
__________________________________________________________________________________________________
// Time Complexity O(n)
class Solution {
public:
void wiggleSort(vector<int> &nums) {
if (nums.size() <= 1) return;
for (int i = 1; i < nums.size(); ++i) {
if ((i % 2 == 1 && nums[i] < nums[i - 1]) || (i % 2 == 0 && nums[i] > nums[i - 1])) {
swap(nums[i], nums[i - 1]);
}
}
}
};
__________________________________________________________________________________________________