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. Iframe跨域Session丢失的问题

    很久之前做的一个使用插件实现了图片批量上传,是通过IFrame加载上传面板的,使用google的chrome上传成功了就没怎么理了,最近同事测试时(使用的是360安全浏览器)老是出现上传不了图片的问题 ...

  2. java并发编程:如何创建线程

    原文:http://www.cnblogs.com/dolphin0520/p/3913517.html 一.Java中关于应用程序和进程相关的概念 在Java中,一个应用程序对应着一个JVM实例(也 ...

  3. 三、freemarker数据、模版指令

    数据类型 1.         直接指定值(字符串.数值.布尔值.集合.Map对象) 2.         字符串:直接指定字符串使用单引号.双引号,字符中间可以使用转义符“\”,如果字符内有大量特殊 ...

  4. android下的数据存储

    android下数据存储的几种方式:(简单讨论) 1.文件 举例:登陆时“记住密码” 因为是基于Linux系统,直接建文件,文件会出现在项目工程:而手机登陆时,应该把文件放在手机里,通常数据放在dat ...

  5. UVALive - 7374 Racing Gems 二维非递减子序列

    题目链接: http://acm.hust.edu.cn/vjudge/problem/356795 Racing Gems Time Limit: 3000MS 问题描述 You are playi ...

  6. JVM 崩溃 Failed to write core dump解决办法 WINDOWS

    JVM 崩溃 Failed to write core dump解决办法 WINDOWS MIT key words: JVM,崩溃,windows,Failed,core dump,虚拟内存 最近从 ...

  7. hdu 3879 Base Station 最大权闭合图

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 A famous mobile communication company is plannin ...

  8. Leetcode#103 Binary Tree Zigzag Level Order Traversal

    原题地址 基本数据结构操作,二叉树的层次遍历. 代码: vector<vector<int> > zigzagLevelOrder(TreeNode *root) { vect ...

  9. [百度空间] ld: add library file reference by path & file name

    By default, -l option will search libraries with lib* prefix in speficied search paths. i.e. 1 ld -o ...

  10. DSP中常用的C语言关键字

    const Ø使用:const 数据类型 变量名: Ø作用:优化存储器的分配,表示变量的内容是常数,不会改变. Ø举例:const char tab[1024]={显示数据}; volatile(易变 ...