[LeetCode] Wiggle Sort 摆动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]....
Example:
Input:nums = [3,5,2,1,6,4]
Output: One possible answer is [3,5,1,6,2,4]
这道题让我们求摆动排序,跟 Wiggle Sort II 相比起来,这道题的条件宽松很多,只因为多了一个等号。由于等号的存在,当数组中有重复数字存在的情况时,也很容易满足题目的要求。这道题先来看一种时间复杂度为 O(nlgn) 的方法,思路是先给数组排个序,然后只要每次把第三个数和第二个数调换个位置,第五个数和第四个数调换个位置,以此类推直至数组末尾,这样就能完成摆动排序了,参见代码如下:
解法一:
class Solution {
public:
void wiggleSort(vector<int>& nums) {
sort(nums.begin(), nums.end());
if (nums.size() <= ) return;
for (int i = ; i < nums.size(); i += ) {
swap(nums[i], nums[i - ]);
}
}
};
这道题还有一种 O(n) 的解法,根据题目要求的 nums[0] <= nums[1] >= nums[2] <= nums[3]....,可以总结出如下规律:
当i为奇数时,nums[i] >= nums[i - 1]
当i为偶数时,nums[i] <= nums[i - 1]
那么只要对每个数字,根据其奇偶性,跟其对应的条件比较,如果不符合就和前面的数交换位置即可,参见代码如下:
解法二:
class Solution {
public:
void wiggleSort(vector<int>& nums) {
if (nums.size() <= ) return;
for (int i = ; i < nums.size(); ++i) {
if ((i % == && nums[i] < nums[i - ]) || (i % == && nums[i] > nums[i - ])) {
swap(nums[i], nums[i - ]);
}
}
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/280
类似题目:
参考资料:
https://leetcode.com/problems/wiggle-sort/
https://leetcode.com/problems/wiggle-sort/discuss/71692/Java-O(N)-solution
https://leetcode.com/problems/wiggle-sort/discuss/71688/4-lines-O(n)-C%2B%2B
https://leetcode.com/problems/wiggle-sort/discuss/71693/My-explanations-of-the-best-voted-Algo
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Wiggle Sort 摆动排序的更多相关文章
- [LeetCode] 280. Wiggle Sort 摆动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [LeetCode] Wiggle Sort II 摆动排序
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- [LeetCode] Wiggle Sort II 摆动排序之二
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- [LintCode] Wiggle Sort 扭动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- Leetcode Wiggle Sort II
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- [LeetCode] Wiggle Sort
Problem Description: Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[ ...
- [LeetCode] 324. Wiggle Sort II 摆动排序 II
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- LeetCode 280. Wiggle Sort (摆动排序)$
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
随机推荐
- UITableViewCell单元格的删除、插入、移动
UITableViewDelegate的方法 设置编辑模式中得cell的编辑样式(删除或插入) - (UITableViewCellEditingStyle)tableView:( ...
- 使用C#,轻松发邮件之QQ邮箱
参考来源 http://www.cnblogs.com/youring2/archive/2008/11/29/1343911.html
- C# 本质论 第四章 方法和参数
要为方法名使用动词或动词短语 递归:递归调用方法 方法重载: try catch
- C#WebBrowrse拦截下载对话框
为了实现这个功能,可算是折腾不少时间,网上搜素出来的结果基本都是如何屏蔽警告对话框.后来请教一个技术大牛(程序员之窗的主要作者Starts_2000),他用C++实现了,他尝试了下C#也没有解决,就忙 ...
- c#使用Split分割换行符 \r\n
c# 使用Split分割 换行符,方法如下(其余方法有空再添加): string str = "aa" + "\r\n" + "bb"; ...
- 继续上篇抢QQ口令红包,抢那招抢不了的红包技巧
- - - - - - - - - - -- - - --长按红包,出现回复,点击回复,那回复里有个表情,直接输入那个表情回复就可以抢了 - - - - - - - - --------------- ...
- FunDA(2)- Streaming Data Operation:流式数据操作
在上一集的讨论里我们介绍并实现了强类型返回结果行.使用强类型主要的目的是当我们把后端数据库SQL批次操作搬到内存里转变成数据流式按行操作时能更方便.准确.高效地选定数据字段.在上集讨论示范里我们用集合 ...
- [原创]如何利用BI搭建电商数据分析平台
某电商是某大型服装集团下的重要销售平台.2015 年,该集团品牌价值达数百亿元,产品质量.市场占有率.出口创汇.销售收入连年居全国绒纺行业第一,在中国有终端店3000多家,零售额80 亿.其羊绒制品年 ...
- 【转】iOS UIApplication详解
1.状态栏UIStateBar的设置是在UIApplication里面设置的,它包含4中风格 2. - (void)beginIgnoringInteractionEvents; (void)endI ...
- NSURLSession网络请求
个人感觉在网上很难找到很简单的网络请求.或许是我才疏学浅 , 所有就有了下面这一段 , 虽然都是代码 , 但是全有注释 . //1/获取文件访问路径 NSString *path=@"ht ...