Wiggle Sort 解答
Question
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].
Solution 1
仔细观察题目,有个条件很重要就是nums[0] <= nums[1] >= nums[2] <= nums[3]....也就是说对于奇书的 i 我们要保证 nums[i - 1] <= nums[i] <= nums[i + 1] 所以第一个想法是1. 先排序 2. 交换每个nums[i] 和 nums[i + 1]
Time complexity O(n log n).
public class Solution {
public void wiggleSort(int[] nums) {
if (nums == null || nums.length < 2)
return;
Arrays.sort(nums);
for (int i = 1; i < nums.length - 1; i = i + 2) {
int tmp = nums[i];
nums[i] = nums[i + 1];
nums[i + 1] = tmp;
}
}
}
Solution 2
分奇偶来思考。
如果i是奇数,那么nums[i]应该大于nums[i - 1],否则交换
如果i是偶数,那么nums[i]应该小于nums[i - 1],否则交换
public class Solution {
public void wiggleSort(int[] nums) {
if (nums == null)
return;
for (int i = 1; i < nums.length; i++) {
if (i % 2 == 1) {
if (nums[i] < nums[i - 1])
swap(nums, i, i - 1);
} else {
if (nums[i] > nums[i - 1])
swap(nums, i, i - 1);
}
}
}
private void swap(int[] nums, int x, int y) {
int tmp = nums[x];
nums[x] = nums[y];
nums[y] = tmp;
}
}
Wiggle Sort 解答的更多相关文章
- [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 Sort 摆动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- Leetcode 280. Wiggle Sort
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [LintCode] Wiggle Sort II 扭动排序之二
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- [LintCode] Wiggle Sort 扭动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- lintcode:Wiggle Sort II
Wiggle Sort II Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] ...
- lintcode:Wiggle Sort
Wiggle Sort Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= ...
- [Locked] Wiggle Sort
Wiggle Sort Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= ...
随机推荐
- Centos 6.5中安装后不能打开emacs的问题
问题的发现过程: 安装了最新的centos版本后发现居然打不开emacs,然后在终端中输入emacs后还是不能打开,出现了下面的提示: emacs: error while loading share ...
- 第24讲 UI_布局 之帧布局 表格布局 绝对布局
第24讲 UI_布局 之帧布局 表格布局 绝对布局 3. FrameLayout(帧布局) 帧布局是从屏幕的左上角(0,0)坐标开始布局,多个组件层叠排序,后一个组件总会将前一个组件所覆盖,除非最后一 ...
- 【转】iOS 解决ipv6问题
解决ipv6的方法有很多种,由于现在国内的网络运营商还在使用ipv4的网络环境,所以appstore应用不可能大范围去修改自己的服务器, 而且国内的云服务器几乎没有ipv6地址. 这里附上苹果开发平台 ...
- (转)iPhone 判断UITableView 滚动到底部
UITableView is a subclass of UIScrollView, and UITableViewDelegate conforms to UIScrollViewDelegate. ...
- Hadoop,HBase集群环境搭建的问题集锦(四)
21.Schema.xml和solrconfig.xml配置文件里參数说明: 參考资料:http://www.hipony.com/post-610.html 22.执行时报错: 23., /comm ...
- 漫话Unity3D(一)
前言 使用Unity已经有将近半年时间了,尽管项目还仅仅是个半成品,可是Unity差点儿相同玩熟了.这里把使用过程中碰到的问题梳理一遍.不会涉及到太过详细的功能和代码,可是假设开发一个网游又都会涉及到 ...
- GridView出现不完整--GridView删除滚动条
GridView显示不完毕的原因是由于,他的外层也套用了一个滑动的控件,解决办法就是重写GridView,是控制GridView不能滚动,就是写一个类继承GridView 代码例如以下 public ...
- 使用android-resource-remover删除项目中无用的资源,减少包的大小
写这篇文章的原因是,一个CSDN的资源链接,Android程序员必备精品资源,在该链接的实用工具集锦中有一个工具吸引了我的注意,那就是android-resource-remover,它的解释是:一个 ...
- Ubuntu中设置静态IP和DNS(转载)
原文地址:http://blog.sina.com.cn/s/blog_669421480102v3bb.html VMware 中使用网络,对虚拟机设置静态IP:在Ubuntu中设置静态IP共两步: ...
- JS高级程序设计学习笔记之基本包装类型
概述 基本类型:string.boolean.number 每当读取一个基本类型的值的时候,后台就会创建一个对应的基本包装类型的对象,从而让我们能够调用一些方法来操作这些数据. 使用new操作符创建的 ...