-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path508.wiggle-sort.cpp
53 lines (51 loc) · 1.24 KB
/
508.wiggle-sort.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Tag: Quick Select, Sort
// Time: O(N)
// Space: O(1)
// Ref: Leetcode-280
// Note: -
// Given an unsorted array `nums`, reorder it **in-place** such that
//
// ```
// nums[0] <= nums[1] >= nums[2] <= nums[3]....
// ```
//
// There may have multiple answers for a question, **you only need to consider one of the possibilities**.
//
// **Example 1:**
//
// ```
// Input: [3, 5, 2, 1, 6, 4]
// Output: [1, 6, 2, 5, 3, 4]
// Explanation: This question may have multiple answers, and [2, 6, 1, 5, 3, 4] is also ok.
// ```
//
// **Example 2:**
//
// ```
// Input: [1, 2, 3, 4]
// Output: [1, 4, 2, 3]
// ```
//
// Please sort the array in place and do not define additional arrays.
class Solution {
public:
/**
* @param nums: A list of integers
* @return: nothing
*/
void wiggleSort(vector<int> &nums) {
// write your code here
int n = nums.size();
for (int i = 1; i < n; i++) {
if (i & 1 == 1) {
if (nums[i] < nums[i - 1]) {
swap(nums[i], nums[i - 1]);
}
} else {
if (nums[i] > nums[i - 1]) {
swap(nums[i], nums[i - 1]);
}
}
}
}
};