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

一开始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. 《转》前端性能优化----yahoo前端性能团队总结的35条黄金定律

    除了自己总结:1. 减少http请求,2.压缩并优化js/css/image 3.尽量静态页面,从简原则 4.代码规范(详见:个人知识体系思维导图) 从yahoo 新学到的: 网页内容 减少http请 ...

  2. node初步一:HTTP请求

    一. 创建pathtest.js文件 var http= require('http' ); var url= require('url' ); function start (){ function ...

  3. github避免每次输入账户密码

    方法1: 显示所有隐藏目录,找到目录./git下的文件config文件,通过文本方式打开,在最前面添加如下两行.之后再次输入一次密码后就会记住账号密码. [credential]     helper ...

  4. ntpath join(path, *paths) 发生UnicodeDecodeError的Bug的解决方案

    最近在学习用Python开发web,使用的是web框架,在win8.1平台下安装SAE和Flask的时候总是在ntpath.py这个UnicodeDecodeError模块报错. 84行 result ...

  5. GIF文件转换为头文件工具

    目的: GIF文件转为头文件 举例: 用UE打开GIF文件,如下图所示:图1 test.gif文件将上面文件内容转化为头文件,放到一个数组里面,内容如下:图2 test.h文件 思路: 从上面可知,将 ...

  6. Java 高效检查一个数组中是否包含某个值

    如何检查一个数组(未排序)中是否包含某个特定的值?在Java中,这是一个非常有用并又很常用的操作.同时,在StackOverflow中,有时一个得票非常高的问题.在得票比较高的几个回答中,时间复杂度差 ...

  7. DELPHI TMS Advanced Charts 3.8.0.3 Full Source D6-XE6 控件分享

    仅供大家学习使用,请大家支持正版!! TMS Advanced Charts 3.8.0.3 Full Source D6-XE6 该控件用来画图标,压缩包里还有FOR INTRAWEB的版本 链接: ...

  8. 判断是否为ie(包含ie11)

    if (!!window.ActiveXObject || "ActiveXObject" in window) { alert("IsIE"); }

  9. bzoj 1006: [HNOI2008]神奇的国度 弦图的染色问题&&弦图的完美消除序列

    1006: [HNOI2008]神奇的国度 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 1788  Solved: 775[Submit][Stat ...

  10. C/C++中的内存对齐 C/C++中的内存对齐

    一.什么是内存对齐.为什么需要内存对齐? 现代计算机中内存空间都是按照byte划分的,从理论上讲似乎对任何类型的变量的访问可以从任何地址开始,但实际情况是在访问特定类型变量的时候经常在特 定的内存地址 ...