Given an unsorted array nums, reorder it in-place such that

nums[0] <= nums[1] >= nums[2] <= nums[3]....
Notice

Please complete the problem in-place.

Example

Given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4].

分析:

排序,从第三个开始,把它和前一个数互换。

 public class Solution {
/**
* @param nums a list of integer
* @return void
*/
public void wiggleSort(int[] nums) {
if (nums == null || nums.length <= ) return; Arrays.sort(nums); for(int i = ; i < nums.length; i+=){
int tmp = nums[i-];
nums[i-] = nums[i];
nums[i] = tmp;
}
}
}

Given an unsorted array nums, reorder it such that

nums[0] < nums[1] > nums[2] < nums[3]....
Notice

You may assume all input has valid answer.

Example

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].

分析:

先sort, 然后用一个指针指向中间那个数,另一个指针指向最末尾那个数,然后依次把数放入到一个新数组里.

 public class Solution {
/**
* @param nums a list of integer
* @return void
*/
public void wiggleSort(int[] nums) {
if (nums == null || nums.length <= ) return;
Arrays.sort(nums);
int[] temp = new int[nums.length]; // 两个指针 p, q. p 指向中间那个数,q指向最右边那个数。
// 不断交替把数放在temp数组里。
int mid = (temp.length + ) / - ;
int end = temp.length - ; for (int i = ; i < temp.length; i++) {
if ((i & ) == ) {
temp[i] = nums[mid--];
} else {
temp[i] = nums[end--];
}
} // put the numbers back to nums.
for (int i = ; i < temp.length; i++) {
nums[i] = temp[i];
}
}
}

还有一种方法,先把array中的median找出来,然后把小于median和大于median的值交叉放入新数组中。

该方法来自: http://buttercola.blogspot.com/2016/01/leetcode-wiggle-sort-ii.html

 public class Solution {
public void wiggleSort(int[] nums) {
if (nums == null || nums.length <= ) {
return;
} int n = nums.length; // Step 1: Find median of the array, return the index of the median
int median = findMedian(nums, , n - , (n - ) / ); // Step 2: 3-way sort, put median in the middle,
// numbers less than median on the left,
// numbers greater than median on the right
int[] temp = new int[n];
int left = ;
int right = n - ; for (int i = ; i < n; i++) {
if (nums[i] < nums[median]) {
temp[left] = nums[i];
left++;
} else if (nums[i] > nums[median]) {
temp[right] = nums[i];
right--;
}
} // add median into the middle
for (int i = left; i <= right; i++) {
temp[i] = nums[median];
} // Step 3: wiggle sort
left = (n - ) / ;
right = n - ; for (int i = ; i < n; i++) {
if ((i & ) == ) {
nums[i] = temp[left];
left--;
} else {
nums[i] = temp[right];
right--;
}
}
} private int findMedian(int[] nums, int lo, int hi, int k) {
if (lo >= hi) {
return lo;
} int pivot = partition(nums, lo, hi);
if (pivot == k) {
return pivot;
} if (pivot > k) {
return findMedian(nums, lo, pivot - , k);
} else {
return findMedian(nums, pivot + , hi, k);
}
} private int partition(int[] nums, int lo, int hi) {
int pivot = nums[lo];
int i = lo + ;
int j = hi; while (i <= j) {
while (i <= j && nums[i] < pivot) {
i++;
} while (i <= j && nums[j] >= pivot) {
j--;
} if (i <= j) {
swap(nums, i, j);
}
} swap(nums, lo, j); return j;
} private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}

Wiggle Sort I & II的更多相关文章

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

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

  2. lintcode:Wiggle Sort II

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

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

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

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

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

  5. [LeetCode] Wiggle Sort II 摆动排序之二

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

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

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

  7. [LeetCode] Wiggle Sort 摆动排序

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

  8. leetcode笔记:Wiggle Sort

    一. 题目描写叙述 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nu ...

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

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

随机推荐

  1. 《Linux内核分析》 第五节 扒开系统调用的三层皮(下)

    <Linux内核分析> 第五节 扒开系统调用的三层皮(下) 20135307 一.给MenusOS增加time和time-asm命令 给MenuOS增加time和time-asm命令需要 ...

  2. Linux内核读书笔记第二周

    什么是系统调用 简单来说,系统调用就是用户程序和硬件设备之间的桥梁.用户程序在需要的时候,通过系统调用来使用硬件设备. 系统调用的存在,有以下重要的意义: 1)用户程序通过系统调用来使用硬件,而不用关 ...

  3. R-CNN阅读笔记

    论文地址:<Rich feature hierarchies for accurate object detection and semantic segmentation> 论文包含两个 ...

  4. 顺序表的C、C++实现

    一个线性表表实现,函数声明放在 line_list.h 头文件汇总,函数定义放在line_list.c 中,main.c 用来测试各个函数. 1.文件 line_list.h // line_list ...

  5. 贝云cms内容管理系统(thinkphp5.0开源cms管理系统)

    byCms包含文章,图片,下载,视频模型,基于thinkphp5.0.9,可无缝升级至thinkphp.1.0,是一套简单,易用的内容管理系统,旨在帮助开发者节约web应用后台开发时间和精力,以最快的 ...

  6. Aqua Data Studio 数据库开发工具

    Aqua Data Studio是一款完整IDE的数据库开发工具,它提供3种主要功能:数据查询与管理工具.比对数据工具与源控制和文件系统的整合工具.帮助你创建,编辑和执行 SQL 的管理工具脚本编写, ...

  7. Queries about less or equal elements CodeForces - 600B(二分)

    You are given two arrays of integers a and b. For each element of the second arraybj you should find ...

  8. bzoj1018/luogu4246 堵塞的交通 (线段树)

    对于一个区间四个角的点,可以用线段树记下来它们两两的联通情况 区间[l,r]通过两个子区间[l,m],[m+1,r]来更新,相当于合并[l,m],[m+1,r],用(m,m+1)这条边来合并 查询a, ...

  9. LibreOJ #2325. 「清华集训 2017」小Y和恐怖的奴隶主(矩阵快速幂优化DP)

    哇这题剧毒,卡了好久常数才过T_T 设$f(i,s)$为到第$i$轮攻击,怪物状态为$s$时对boss的期望伤害,$sum$为状态$s$所表示的怪物个数,得到朴素的DP方程$f(i,s)=\sum \ ...

  10. (转)丢掉鼠标吧,使用最好用的eclipse快捷键

    背景:eclipse作为自己经常使用的一款开发工具,熟练运用,能够达到事半功倍的效果.下面这篇文章总结了一些平时经常要使用的快捷键,十分的方便. 介绍Eclipse快捷键的文章很多,但大多都不详细,且 ...