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 解答的更多相关文章

  1. [LeetCode] Wiggle Sort II 摆动排序

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

  2. [LeetCode] Wiggle Sort II 摆动排序之二

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

  3. [LeetCode] Wiggle Sort 摆动排序

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

  4. Leetcode 280. Wiggle Sort

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

  5. [LintCode] Wiggle Sort II 扭动排序之二

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

  6. [LintCode] Wiggle Sort 扭动排序

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

  7. lintcode:Wiggle Sort II

    Wiggle Sort II Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] ...

  8. lintcode:Wiggle Sort

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

  9. [Locked] Wiggle Sort

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

随机推荐

  1. 【剑指offer】面试题39扩展:平衡二叉树

    题目: 输入一棵二叉树,判断该二叉树是否是平衡二叉树. 思路: 直观的思路是,判断根结点的左子树.右子树高度差是否小于1. 为避免多次访问同一结点,应该用后序遍历的方式访问. 注意:加号优先级高于条件 ...

  2. IOS UIlabel设置文本距离边框距离

    自定义UILabel 继承 UILabel 重写drawTextInRect 方法具体如下: CGRect rect = CGRectMake(rect.origin.x + 5, rect.orig ...

  3. vbox下centos安装增加功能失败

    一般都是:unable to find the sources of your current Linux kernel. 先尝试这个吧:yum install kernel kernel-heade ...

  4. CentOS 6.3中安装OpenCV2.3.1

    下面为自己测试可用的OpenCV在Linux下的安装步骤 .检查并安装相关程序,确保gtk安装成功,否则无法显示图片 yum install gcc-c++ yuminstall gtk-devel. ...

  5. Building bridges_hdu_4584(排序).java

    Building bridges Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) ...

  6. android面试题之七

    三十六.请解释下在单线程模型中Message.Handler.Message Queue.Looper之间的关系. 简单的说,Handler获取当前线程中的looper对象,looper用来从存放Me ...

  7. Win7x64安装Oracle11201x64 解决PLSQL Developer无法找到oci问题

    http://blog.sina.com.cn/s/blog_4c7628c40101cf56.html http://blog.csdn.net/shenkxiao/article/details/ ...

  8. onblur判断数字

    window.onload = function () { document.getElementById('text1').onblur = function () { if (isNaN(docu ...

  9. 不指定order by时Sql的排序

    在sql中不指定Order by,排序是按照主键吗?答案是不一定.举个例子:   查询AttendanceEmpRank表,主键是AttendanceEmployeeRankId,而且是聚集索引   ...

  10. PHP MAIL DEMO(程序代码直接发送邮件)

    php代码 <?php // 收件人邮箱地址 $to = 'xxxxxx@qq.com'; // 邮件主题 $title = '测试邮件发送'; // 邮件内容 $msg = '这是一封测试邮件 ...