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. 每日scrum(6)

    今天是小组正式冲刺的第六天,软件的各种结尾工作,还有一些模块就已经全部实现了: 遇到的问题主要是对于自己能力的担忧,以前总是想,如果自己努力,就会怎样成功,其实并不是那样,小小的距离就是很远的能力差距 ...

  2. 重温redis命令

    redis是已知的性能最快的key-value 数据库. 1.key相关命令 exists key :检查指定的key是否存在 1表示存在 0表示不存在 del key1,key2,key3....: ...

  3. PAT 甲级 1029 Median

    https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968 Given an increasing se ...

  4. Ubuntu16解锁root

    administrator@rgqancy:~$ sudo passwd -u root [sudo] administrator 的密码: 对不起,请重试. [sudo] administrator ...

  5. vue 使用element-ui upload文件上传之后怎么清空

    首先上传组件中一定要绑定这两个属性: ref,和 :file-list,如果没有ref,即使 用 this.$refs.upload.clearFiles()也不行,因为这时候this.$refs为空 ...

  6. 扩展名为DBF的是什么文件啊?

    扩展名为DBF的文件: .dbf文件是dBase和FoxPro所使用的数据库格式,在没有这两种软件的情况下,可以使用Excel打开文件.在Excel的“打开”文件的对话框中,选择文件类型为“dBase ...

  7. Jenkins之手动安装

    Download and run Jenkins Download Jenkins. Open up a terminal in the download directory. Run java -j ...

  8. Optimal Milking POJ - 2112 (多重最优匹配+最小费用最大流+最大值最小化 + Floyd)

      Optimal Milking Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 19347   Accepted: 690 ...

  9. linux下转换windows文件格式为unix sed -i 's/\r//' <filename> 转化为unix格式

    sed -i 's/\r//' <filename> 转化为unix格式

  10. 【刷题】BZOJ 4254 Aerial Tramway

    Description You own a park located on a mountain, which can be described as a sequence of n points ( ...