题目:

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].

链接: http://leetcode.com/problems/wiggle-sort/

题解:

Wiggle排序数组。按照题意写就可以了。 还可以简化,要多学习Stefan的代码。

Time Complexity - O(n), Space Complexity - O(1)

public class Solution {
public void wiggleSort(int[] nums) {
for(int i = 1; i < nums.length; i++) {
if(i % 2 == 1) {
if(nums[i] < nums[i - 1]) {
swap(nums, i);
}
} else {
if(i != 0 && nums[i] > nums[i - 1]) {
swap(nums, i);
}
}
}
} private void swap(int[] nums, int i) {
int tmp = nums[i - 1];
nums[i - 1] = nums[i];
nums[i] = tmp;
}
}

二刷:

方法和一刷一样。在i % 2 == 1或者 == 0的时候作交换的判断,交换完毕以后仍然保持这是一个本地化操作就可以了。交换的时候是用 i和 i - 1来交换

Java:

Time Complexity - O(n), Space Complexity - O(1)

public class Solution {
public void wiggleSort(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
for (int i = 1; i < nums.length; i++) {
if (i % 2 == 1) {
if (nums[i] < nums[i - 1]) {
swap(nums, i);
}
} else {
if (nums[i] > nums[i - 1]) {
swap(nums, i);
}
}
}
} private void swap(int[] nums, int i) {
int tmp = nums[i - 1];
nums[i - 1] = nums[i];
nums[i] = tmp;
}
}

三刷:

跟前面一样,也是直接编写。

Java:

public class Solution {
public void wiggleSort(int[] nums) {
if (nums == null || nums.length < 2) return;
for (int i = 1; i < nums.length; i++) {
if ((i % 2 == 0 && nums[i] > nums[i - 1]) || (i % 2 == 1 && nums[i] < nums[i - 1])) {
int tmp = nums[i];
nums[i] = nums[i - 1];
nums[i - 1] = tmp;
}
}
}
}

Reference:

https://leetcode.com/discuss/57113/java-o-n-solution

https://leetcode.com/discuss/57206/java-o-n-10-lines-consice-solution

https://leetcode.com/discuss/60824/java-python-o-n-time-o-1-space-solution-3-lines

https://leetcode.com/discuss/57118/easy-code-of-python

280. Wiggle Sort的更多相关文章

  1. leetcode 280.Wiggle Sort 、324. Wiggle Sort II

    Wiggle Sort: 注意:解法一是每次i增加2,题目不是保证3个3个的情况,而是整个数组都要满足要求. 解法一错误版本: 如果nums的长度是4,这种情况下nums[i+1]会越界.但是如果你用 ...

  2. Leetcode 280. Wiggle Sort

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

  3. LeetCode 280. 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. 【LeetCode】280. Wiggle Sort 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序后交换相邻元素 日期 题目地址:https://l ...

  6. LeetCode 280. Wiggle Sort C#

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

  7. 【leetcode】280.Wiggle Sort

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

  8. [LeetCode] 324. Wiggle Sort II 摆动排序 II

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

  9. [LeetCode] Wiggle Sort 摆动排序

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

随机推荐

  1. CentOS安装Nexus(Maven私有库)详细配置及上传本地jar到私服

    Nexus原理 Maven的原理就是将jar从远程中央仓库下载到PC磁盘的本地仓库,当本地仓库没有发现需要的jar就会去Maven默认的远程中央仓库Maven Central(由Apache维护)中寻 ...

  2. 20145105 《Java程序设计》第2周学习总结

    20145105 <Java程序设计>第2周学习总结 教材学习内容总结 第三章主要的学习内容与c语言有很多相似的地方,讲述了Java的基本语法.其中涵盖: 1. 类型: - short整数 ...

  3. 四则运算2--c++

    1.设计思路: 上篇已写,不在解释..... 2.源代码: #include<iostream.h>#include<stdlib.h>#include "time. ...

  4. Travis-CI的进一步使用

    今天主要对.travis.yml文件和makefile进行进一步的了解: 1.在.travis.yml文件中添加了给linux系统中安装了cppunit库的语句,使能够持续集成写过的单元测试的代码.主 ...

  5. JS中的forEach、$.each、map方法

    forEach是ECMA5中Array新方法中最基本的一个,就是遍历,循环.例如下面这个例子: [1, 2 ,3, 4].forEach(alert); 等同于下面这个for循环 var array ...

  6. opencv 2.4.9+pcl 1.6+vs2010+win7 32开发环境配置

    最近在做图像方面的开发,需要对软件开发平台进行配置,我查找了关于这些方面的内容,由于软件版本很多,每个人的开发平台又不一样所以在对平台进行搭建过程中遇到了很多问题,下面我将我搭建平台的流程做一个记录. ...

  7. OS X 使用技巧——不用鼠标就能打开应用程序

    如果要打开的应用程序没有保留在Dock栏里,一种快速启动它的办法是按住Control+Space键后再输入应用程序的名称.按Control+Space键会开启聚光灯(Spotlight)搜索工具,它会 ...

  8. 浅谈GitHub

    Git 是一个面向开源及私有软件项目的托管平台,因为只支持Git作为唯一的版本库格式进行托管,故名GitHub. Gith是一个基于 git 的社会化代码分享社区,所谓 social coding.你 ...

  9. [工作积累] Android dynamic library & JNI_OnLoad

    Bionic libc doesn't load dependencies for current .so file (diff from Windows or Linux) so a explici ...

  10. 【转载】MySQL索引原理及慢查询优化

    原文链接:美团点评技术团队:http://tech.meituan.com/mysql-index.html MySQL凭借着出色的性能.低廉的成本.丰富的资源,已经成为绝大多数互联网公司的首选关系型 ...