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

一开始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. C# 绘制窗体客户非客户区要用WM_PAINT和WM_NCPAINT

    窗体分为两部分:客户区(Client area)和非客户区(Non-Client area) WM_PAINT消息.OnPaint()方法.GetDC()API函数都是处理窗体客户区绘制的   而标题 ...

  2. java学习之反射机制

    java语言区别于C,C++等准静态语言的最大特点就是java的反射机制.静态语言的最直接定义就是不能在运行时改变程序结构或变量的类型.按照这样的定义,python,ruby是动态语言,C,C++,J ...

  3. adodb配置与使用

    =========================================php100:80:ADODB PHP数据库万能引擎类 ADODB PHP数据库介绍与特点 ADODB 是一种兼容的各 ...

  4. C#Winform开发平台企业版V4.0功能表

    企业版V4.0 - 功能列表及模板窗体 C/S系统开发框架-企业版 V4.0 (Enterprise Edition) 简介: http://www.csframework.com/cs-framew ...

  5. Forward reference vs. forward declaration

    Q:Im a bit confused. What is the difference between forward declaration and forward reference? Forwa ...

  6. bitVector@ java bit自我practice##Q&A:为何int 来初始化size of bitVector?long,甚至是BigInteger等策略

    /* * BitSets are packed into arrays of "words." Currently a word is * a long, which consis ...

  7. smali 语法之if语句

    # virtual methods .method public onClick(Landroid/view/View;)V .locals 3 .parameter "v" .p ...

  8. mongodb常用命令【转】

    mongodb由 C++编写,其名字来自humongous这个单词的中间部分,从名字可见其野心所在就是海量数据的处理.关于它的一个最简洁描述为:scalable, high-performance, ...

  9. 代理服务器基本知识普及代理IP使用方法!

    本文并未从专业角度进行详细讲解,而是从应用的角度出发来普及一些代理服务器的基本知识.文章明显是搜集多方资料的拼凑,而且比较老了,但往往越老的东西越接近事物的本质,更容易窥探到原理,对于刚接触的人来说, ...

  10. JSP环境配置

    为免以后忘记,记下了. Jdk在C盘,tomcat在D盘. 1.JAVA_HOME C:\Program Files\Java\jdk1.7.0_07 2.CATALINA_HOME D:\apach ...