利用中位数的概念,中位数就是将一组数分成2等份(若为奇数,则中位数既不属于左也不属于右,所以是2等份),其一组数中任何一个元素都大于等于另一组数

那么我们是不是只要一左一右配合着插入,就保证了差值+-+-+-的要求?

由于题目输入限制了,必定存在解,所以此处我们不需要担心如果重复中位数太多,出现一左一右相等的情况

算法步骤:

1)找到中位数

  利用lc215 Kth Largest...,将k=(nums.length+1) / 2,可以在o(n)内求出中位数

2)一左一右插入

  0 2 4 .. 插大于中位数的

  1 3 5 .. 插小于中位数的

  考虑到中位数可能有重复的情况

  我们从后往前插入小于中位数的值

  从前往后插入大于中位数的值

  剩下的空位补上中位数

代码中为了省事找中位数的部分用的o(klogn)的方法,详情可以看之前博客

 class Solution {
public void wiggleSort(int[] nums) {
int median = findKthLargest(nums, (nums.length+1) / 2);
int left = 1;
int right = nums.length % 2 == 0 ? nums.length-2 : nums.length-1; int[] tmp = new int[nums.length];
for(int i=0; i<nums.length; i++){
if(nums[i] > median){
tmp[left] = nums[i];
left += 2;
}else if(nums[i] < median){
tmp[right] = nums[i];
right -= 2;
}
} while(left < nums.length){
tmp[left] = median;
left += 2;
}
while(right >= 0){
tmp[right] = median;
right -= 2;
} System.arraycopy(tmp, 0, nums, 0, nums.length);
}
public int findKthLargest(int[] nums, int k) {
if(nums.length == 0)
return 0;
PriorityQueue<Integer> pq = new PriorityQueue<>(Comparator.reverseOrder());
int res = 0;
for(int num : nums)
pq.offer(num); while(k != 0){
res = pq.poll();
k--;
} return res;
} }

leetcode 324 Wiggle Sort 2的更多相关文章

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

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

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

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

  3. LeetCode 280. Wiggle Sort (摆动排序)$

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  4. [LeetCode] 280. Wiggle Sort 摆动排序

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  5. Leetcode 280. Wiggle Sort

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  6. 324 Wiggle Sort II 摆动排序 II

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

  7. 324. Wiggle Sort II

    这个题真是做得我想打人了.我生起气来连自己都打. 一开始quick select没啥可说的.但是in place把老命拼了都没想出来.. 看网上的答案是3 partition,MAP式子一看就由衷地反 ...

  8. LeetCode 280. Wiggle Sort C#

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  9. [LeetCode] Wiggle Sort II 摆动排序

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

随机推荐

  1. PHP缓存技术的应用1

    // 自定义缓存类 class Cache_Filesystem { // 缓存写保存 function set ($key, $data, $ttl) { //打开文件为读/写模式 $h = fop ...

  2. android gradle 和gradle plugin

    android gradle 和gradle plugin 1.安装完AS3.5.2创建完项目一运行,报了如下错误 Error:Could not find com.android.tools.bui ...

  3. 牛客多校第五场 G subsequence 1 最长公共子序列/组合数

    题意: 给定两个由数字组成的序列s,t,找出s所有数值大于t的子序列.注意不是字典序大. 题解: 首先特判s比t短或一样长的情况. 当s比t长时,直接用组合数计算s不以0开头的,长度大于t的所有子序列 ...

  4. System.Web.Mvc.HttpHeadAttribute.cs

    ylbtech-System.Web.Mvc.HttpHeadAttribute.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, ...

  5. IENumerable_Test

    using System; using System.Collections; using System.Collections.Generic; using System.Linq; using S ...

  6. js基础应用-打字机,震动窗口

    js基础应用一,窗口震动 html+js代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8 ...

  7. 面试系列13 redis都有哪些数据类型

    (1)string 这是最基本的类型了,没啥可说的,就是普通的set和get,做简单的kv缓存 (2)hash 这个是类似map的一种结构,这个一般就是可以将结构化的数据,比如一个对象(前提是这个对象 ...

  8. virtualbox虚拟机下的cdlinux找不到无线网卡的解决方法

    virtualbox虚拟机下的cdlinux找不到无线网卡的解决方法 自己解决了,记录一下. cdlinux 带reaver1.4的版本 http://pan.baidu.com/share/link ...

  9. SUMMARY | 二分查找

    package Search; public class biSearch { //标准的二分查找 public static int stdBiSearch(int[] array,int keyV ...

  10. C3P0连接池参数配置

    <!--acquireIncrement:链接用完了自动增量3个. --> <property name="acquireIncrement">3</ ...