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. MVVMLight leaning note

    Learning Note For MvvmLight MvvmLight quitstart refer link1 : MVVMLight HelloWorld *** mc:Ignorable ...

  2. Python实现C4.5(信息增益率)

    Python实现C4.5(信息增益率) 运行环境 Pyhton3 treePlotter模块(画图所需,不画图可不必) matplotlib(如果使用上面的模块必须) 计算过程 st=>star ...

  3. Matlab实现加性高斯白噪声信道(AWGN)下的digital调制格式识别分类

    Matlab实现加性高斯白噪声信道(AWGN)下的digital调制格式识别分类 内容大纲 加性高斯白噪声信道(AWGN)下的digital调制格式识别分类 (1. PSK; 2. QPSK; 3.8 ...

  4. UML详解

    学习c++必不可少UML,UML从考虑系统的不同角度出发,定义了用例图.类图.对象图.状态图.活动图.序列图.协作图.构件图.部署图等9种图.这些图从不同的侧面对系统进行描述.系统模型将这些不同的侧面 ...

  5. P3382: [Usaco2004 Open]Cave Cows 3 洞穴里的牛之三

    首先,我们先确定,最长的曼哈顿距离只可能为 x1+y2-(x2+y2) 和 x1-y1-(x2-y2) 所以我们只需要维护四个值, 分别代表 max(x+y) ; max(x-y) ; min(x+y ...

  6. Min Stack

    Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constan ...

  7. VMWare Workstation 11安装CentOS7,附图 [原创]

    1.新建虚拟机 2.新建虚拟机向导,选择典型 3.选择稍后安装操作系统 4.选择linux版本,注意:宿主系统是64位的,此处就得选64位:宿主系统是32位的,此处就得选32位 5.选择路径 6.指定 ...

  8. 使用cronolog切割tomcat catalina.out文件

    今天查看之前配置的tomcat发现catalina.out文件已经增大到接近5G,过不了多久就会将所在分区撑爆. 搜集了一下,大部分都使用cronolog切割catalina.out文件.按照这个方式 ...

  9. 【Validate Binary Search Tree】cpp

    题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...

  10. vs2012 condition_variable notify_one 崩溃

    vs2012项目中用到 condition_variable系统方法,程序运行过程过程中偶尔出现notify_one崩溃, 程序运行的服务器系统版本是windows server 2008 R2 SP ...