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内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.计算机是如何工作的 ( ...

  2. Linux第三周学习总结——构造一个简单的Linux系统MenuOS

    第三周学习总结--构造一个简单的Linux系统MenuOS 作者:刘浩晨 [原创作品转载请注明出处] <Linux内核分析>MOOC课程http://mooc.study.163.com/ ...

  3. C++:多态浅析

    1.多态 在C++中由两种多态性: • 编译时的多态性:通过函数的重载和运算符的重载来实现的 • 运行时的多态性:通过类继承关系和虚函数来实现的 特别注意: a.运行时的多态性是指程序执行前,无法根据 ...

  4. Beta阶段冲刺-5

    一. 每日会议 1. 照片 2. 昨日完成工作 3. 今日完成工作 4. 工作中遇到的困难 杨晨露:现在我过的某种意义上挺滋润的,没啥事了都.......咳,困难就是前端每天都在想砸电脑,我要怎么阻止 ...

  5. UART协议总结

    之前一直使用UART作为单片机之间以及和计算机的简单通信,但一直没有研究过该协议的内部原理.今天刚买了一个逻辑分析仪,于是使用该分析仪对UART数据进行分析,以便更好的理解UART协议原理. UART ...

  6. JavaScript ES6中export及export default的区别以及import的用法

    本文原创地址链接:http://blog.csdn.net/zhou_xiao_cheng/article/details/52759632,未经博主允许不得转载. 相信很多人都使用过export.e ...

  7. 2013长春网赛1005 hdu 4763 Theme Section(kmp应用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4763 题意:给出一个字符串,问能不能在该串的前中后部找到相同的子串,输出最长的字串的长度. 分析:km ...

  8. sniffer 和 debug flow

    sniffer 和 debug flow sniffer 和 debug flow 复制模板,直接修改IP即可使用: diagnose sys session filter clear diagnos ...

  9. word 里面没输入法

    文件,选项,高级,输入法控制处于活动状态   ,有勾选就去掉,无勾选就勾上,确定后重开word即可

  10. 【题解】 Codeforces 919F A Game With Numbers(拓扑排序+博弈论+哈希)

    懒得复制,戳我戳我 Solution: 我感觉我也说不太好,看Awson的题解吧. 说一点之前打错的地方: 连边存的是hash后的数组下标 if(ans[ num( C[a.hash()] , C[b ...