这个题真是做得我想打人了。我生起气来连自己都打。

一开始quick select没啥可说的。但是in place把老命拼了都没想出来。。

看网上的答案是3 partition,MAP式子一看就由衷地反胃。。

老子不管了,就O(n)。。

public class Solution
{
public void wiggleSort(int[] nums)
{
if(nums.length <= 1 ) return;
if(nums.length == 2)
{
if(nums[0] > nums[1]) swap(0,1,nums);
return;
} //start of larger half
int m = (nums.length+1)/2; int mVal = nums[quickSelect(nums,m,0,nums.length-1)]; int[] res = new int[nums.length]; int a = 0, b = nums.length-1;
for(int i = 0; i < res.length;i++)
{
if(nums[i] < mVal)
{
res[a] = nums[i];
a++;
}
else if(nums[i] > mVal)
{
res[b] = nums[i];
b--;
} } while(a < m) res[a++] = mVal;
while(b >= m) res[b--] = mVal;
//System.out.println(a + " " + b + " " + m + " " + mVal);
b = nums.length-1;
a-=1;
for(int i = 0; i < nums.length;i++)
{ if(i%2 == 0)
{
nums[i] = res[a];
a--; }
else
{
nums[i] = res[b];
b--; }
} return; } public int quickSelect(int[] nums, int m, int L, int R)
{
int pIndex = awesomePick(L,R,nums);
int pVal = nums[pIndex]; swap(R,pIndex,nums);
int tempIndex = L;
for(int i = L; i < R;i++)
{
if(nums[i] <= pVal) swap(i,tempIndex++,nums);
}
swap(tempIndex,R,nums); if(tempIndex == m) return tempIndex;
else if(tempIndex < m) return quickSelect(nums,m,tempIndex+1,R);
else return quickSelect(nums,m,L,tempIndex-1); } public void swap(int a, int b, int[] nums)
{
int temp = nums[a];
nums[a] = nums[b];
nums[b] = temp;
} public int awesomePick(int L, int R, int[] nums)
{
int a = nums[L];
int b = nums[R];
int M = L+(R-L)/2;
int c = nums[M]; if( a > b )
{
if( b > c ) return R;
else if(a > c) return M;
else return L; }
//b > a
else
{
if(a > c) return L;
else if(b > c) return M;
else return R;
}
}
}

324. Wiggle Sort II的更多相关文章

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

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

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

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

  3. 324 Wiggle Sort II 摆动排序 II

    给定一个无序的数组nums,将它重新排列成nums[0] < nums[1] > nums[2] < nums[3]...的顺序.例子:(1) 给定nums = [1, 5, 1, ...

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

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

  5. lintcode:Wiggle Sort II

    Wiggle Sort II Given an unsorted array nums, reorder it such that 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 Wiggle Sort II

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

  9. [Swift]LeetCode324. 摆动排序 II | Wiggle Sort II

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

随机推荐

  1. Javascript中bind()方法的使用与实现

    对于bind,我愣了下,这个方法常用在jquery中,用于为被选元素添加一个或多个事件处理程序. 查了下手册,发现bind的作用和apply,call类似都是改变函数的execute context, ...

  2. Java RMI 框架_远程方法调用(2016-08-16)

    概念: Java RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java 虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可 ...

  3. php 单引号与双引号区别

    一.单引号与双引号区别 1." "双引号里面的字段会经过编译器解释,然后再当作HTML代码输出. 2.' '单引号里面的不进行解释,直接输出. 从字面意思上就可以看出,单引号比双引 ...

  4. wamp5.2 升级到wamp5.3 (转载)

    1.  停止WAMP服务器. 2.  去网站windows.php.net 下载php5.3.5 the VC6 Thread Safe build. 不要下载THE INSTALLER. 3.  在 ...

  5. MYSQL 不排序

    mysql: SELECT * FROM EVENT WHERE eventId IN(443,419,431,440,420,414,509) ORDER BY INSTR(',443,419,43 ...

  6. CentOS 6.4 64位 搭建MySQL-Cluster 7.3.8 集群

    准备:1.下载 MySQL-Cluster-gpl-7.3.8-1.el6.x86_64.rpm-bundle.tar 下载地址: http://mirrors.sohu.com/mysql/MySQ ...

  7. [转] 属性选择器.mark

    CSS 2 引入了属性选择器. 属性选择器可以根据元素的属性及属性值来选择元素. 简单属性选择 如果希望选择有某个属性的元素,而不论属性值是什么,可以使用简单属性选择器. 例子 1 如果您希望把包含标 ...

  8. css3多行省略号

    -webkit-line-clamp 概述: -webkit-line-clamp 是一个 不规范的属性(unsupported WebKit property),它没有出现在 CSS 规范草案中. ...

  9. Maven介绍,包括作用、核心概念、用法、常用命令、扩展及配置

    由浅入深,主要介绍maven的用途.核心概念(Pom.Repositories.Artifact.Build Lifecycle.Goal).用法(Archetype意义及创建各种项目).maven常 ...

  10. rm加转义很危险

    rm -r 想转义一个空格字符,转得不对 -r, -R, --recursive 递归删除目录及其内容 连续rm了n个不想rm的文件夹.%>_<% 想起来以前有人也因为rm的失误把整个wo ...