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. c语言结构体数组引用

    struct dangdang { ]; ]; ]; int num; int bugnum; ]; ]; double RMB; }dbdd[]={{,,}, {,,} };//初始化 void m ...

  2. java之File

    1.文件创建.重命名.删除 code: package com.test; import java.io.File; import java.io.IOException; public class ...

  3. WebService- 使用 CXF 开发 SOAP 服务

    选框架犹如选媳妇,选来选去,最后我还是选了“丑媳妇(CXF)”,为什么是它?因为 CXF 是 Apache 旗下的一款非常优秀的 WS 开源框架,具备轻量级的特性,而且能无缝整合到 Spring 中. ...

  4. rac_进行grid自检时提示运行runfixup.sh脚本一例

    原创作品,出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明下面出处,否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlo ...

  5. 为iPhone6设计自适应布局(一)

    译者的话:本文是自适应布局的巩固篇,所以对布局约束的添加操作步骤等没有详细的说明.如果看着吃力的话请先移步Swift自适应布局(Adaptive Layout)教程. Apple从iOS6加入了Aut ...

  6. public,private,protected的区别

    一,public,private,protected的区别public:权限是最大的,可以内部调用,实例调用等.protected: 受保护类型,用于本类和继承类调用.private: 私有类型,只有 ...

  7. [转]MVP模式开发

    转自:http://www.jianshu.com/p/f7ff18ac1c31 基于面向协议MVP模式下的软件设计-(iOS篇) 字数9196 阅读505 评论3 喜欢11 基于面向协议MVP模式下 ...

  8. lambda语法

    (参数列表) => 表达式或者语句块 s => (s.IndexOf("a") > -1 其中:参数个数:可以有多个参数,一个参数,或者无参数.表达式或者语句块: ...

  9. table细线

    table { border-collapse: collapse; } table td { border: 1px solid #CCCCCC; }

  10. July-程序员面试、算法研究、编程艺术、红黑树、数据挖掘5大经典原创系列集锦与总结

    程序员面试.算法研究.编程艺术.红黑树.数据挖掘5大经典原创系列集锦与总结 http://blog.csdn.net/v_july_v/article/details/6543438