您的位置:首页 > 其它

[LeetCode] Wiggle Sort

2015-09-10 12:57 155 查看
Problem Description:

Given an unsorted array
nums
, reorder it in-place such that
nums[0] <= nums[1] >= nums[2] <= nums[3]...
.

For example, given
nums = [3, 5, 2, 1, 6, 4]
, one possible answer is
[1, 6, 2, 5, 3, 4]
.

The final sorted
nums
needs to satisfy two conditions:

If
i
is odd, then
nums[i] >= nums[i - 1]
;

If
i
is even, then
nums[i] <= nums[i - 1]
.

The code is just to fix the orderings of
nums
that do not satisfy 1 and 2.

class Solution {
public:
void wiggleSort(vector<int>& nums) {
int n = nums.size();
for (int i = 1; i < n; i++)
if (((i & 1) && nums[i] < nums[i - 1]) || (!(i & 1) && nums[i] > nums[i - 1]))
swap(nums[i], nums[i - 1]);
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: