Wiggle Sort II

Given an unsorted array nums, reorder it such that

nums[0] < nums[1] > nums[2] < nums[3]....
注意事项

You may assume all input has valid answer.

样例

Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6].

Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2, 3, 1, 3, 1, 2].

挑战

Can you do it in O(n) time and/or in-place with O(1) extra space?

解题

参考链接

先进行排序,然后从中位数mid开始

mid放在第一个位置 len-1放在第二个位置

mid-1放在第三个位置,len-2放在第四个位置

一直进行下去。。。

这样的思想就是让大数和小数交叉排列

public class Solution {
/**
* @param nums a list of integer
* @return void
*/
public void wiggleSort(int[] nums) {
// Write your code here
if(nums == null || nums.length == 0)
return;
Arrays.sort(nums);// 排序
int[] temp = new int[nums.length];
int mid = nums.length%2==0?nums.length/2-1:nums.length/2;// 中间下标
int index = 0;
for(int i=0;i<=mid;i++){
temp[index] = nums[mid-i];
if(index+1<nums.length)
temp[index+1] = nums[nums.length-i-1];
index = index+2;
}
for(int i=0;i<nums.length;i++){
nums[i] = temp[i];
}
}
}

lintcode:Wiggle Sort II的更多相关文章

  1. lintcode:Wiggle Sort

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

  2. leetcode笔记:Wiggle Sort

    一. 题目描写叙述 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nu ...

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

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

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

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

  5. LintCode 508: Wiggle Sort

    LintCode 508: Wiggle Sort 题目描述 给你一个没有排序的数组,请将原数组就地重新排列满足如下性质 nums[0] <= nums[1] >= nums[2] < ...

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

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

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

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

  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. lintcode:数字组合 II

    数字组合 II 给出一组候选数字(C)和目标数字(T),找出C中所有的组合,使组合中数字的和为T.C中每个数字在每个组合中只能使用一次. 注意事项 所有的数字(包括目标数字)均为正整数. 元素组合(a ...

随机推荐

  1. 读取、添加、删除、修改配置文件 如(Web.config, App.config)

    private Configuration config; public OperateConfig() : this(HttpContext.Current.Request.ApplicationP ...

  2. DHCP Server软件使用教程

    DHCP Server软件使用教程 前提网络环境配置 电脑连接上wifi 网络和共享中心中更改适配器,共享无线网卡给以太网网卡 手动设置以太网网卡ipv4地址为192.168.1.1,子网掩码为255 ...

  3. PB中无法插入ole控件,解决办法

    cmd /c for %i in (%windir%\system32\*.ocx) do regsvr32.exe /s %icmd /c for %i in (%windir%\system32\ ...

  4. 网络笔记02数据解析 -1-JSON解析

    1.JSON解析 1.JSON介绍 JSON是最网络传输数据最为广泛的数据格式,没有之一,出身草根,是Javascript的子集,专门负责描述数据格式.Javascript是网页开发的一种脚本语言,和 ...

  5. MD5值算法原理

    MD5原理说明 一.MD5算法介绍. MD5,即“Message-Digest Algorithm 5(信息-摘要算法)”,从名字来看就知道它是从MD3.MD4发展而来的一种加密算法,其主要通过采集文 ...

  6. c语言中static 用法总结(转)

    惨痛教训: 假设在test.h中定义了一个static bool g_test=false; 若test1.c和test2.c都包含test.h,则test1.c和test2.c分别生成两份g_tes ...

  7. “我爱淘”第二冲刺阶段Scrum站立会议1

    完成任务: 完成了webservice的配置与测试,实现了在客户端的搜索功能,并且可以实现图书的发布功能,就是将图书的信息添加到数据库中. 计划任务: 在客户端实现分类功能,通过学院的分类查看书籍. ...

  8. LabView调用C#混合模式dll

    在一些特定要求下,我们的C#可能需要制作dll给LabView进行调用,并且我们不能够保证C#的程序是完全自己写而不调用第三方的dll库.很多时候我们需要使用诸如Sqlite.Net.AForge.N ...

  9. Elasticsearch 权威指南 NESTAPI地址

    Elasticsearch 权威指南:http://fuxiaopang.gitbooks.io/learnelasticsearch/content/index.html NEST:http://n ...

  10. 在一台电脑访问另一台电脑的mysql数据库,并增加和剥夺权限

    1.    假设MySQL服务器安装在ip地址为192.168.105.3的主机上面 2.    再假设客户端安装在ip为192.168.105.100的机子上 3. 首先在ip为192.168.10 ...