【LeetCode】324.Wiggle Sort II(Medium)解题报告
题目地址:https://leetcode.com/problems/wiggle-sort-ii/description/
题目描述:
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]….
Example:
(1) Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6].
(2) Given nums = [1, 3, 2, 2, 3, 1], one possible answer is [2, 3, 1, 3, 1, 2].
Note: You may assume all input has valid answer.
Follow Up: Can you do it in O(n) time and/or in-place with O(1) extra space?
Solution1:
/*
答案有很多种,只返回一种就可以了
非常非常重要的一道题,一定要弄懂
给两种解法
第一种: 先排序,中位数左边的正序插,右边的倒序插,都是向中间方向
time : O(nlogn)
space : O(n)
*/
class Solution {
public void wiggleSort(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
int mid = (n - 1) / 2;
int index = 0;
int[] temp = new int[n];
for(int i=0 ; i<=mid ; i++){
temp[index] = nums[mid - i];
if(index + 1 < n){
temp[index + 1] = nums[n - i - 1];
}
index += 2;
}
System.arraycopy(temp,0,nums,0,n);
}
}
Solution2:
/*
答案有很多种,只返回一种就可以了
非常非常重要的一道题,一定要弄懂
给两种解法
第二种: 很巧妙的一种方法,超级厉害
还是找中位数,但是不用nlogn的排序,借用215题 Kth largest element in an array
然后运用方法一的思想。
大于中位数 : 从左往右 奇数位
小于中位数 : 从右往左 偶数位
中位数最后放
reference : https://leetcode.com/problems/wiggle-sort-ii/discuss77682
Original idx: 0 1 2 3 4 5
Mapped idx: 1 3 5 0 2 4
Array: 13 6 4 5 2 5
5 6 4 13 2 5
r
i
l
median = 5
nums[] = 5
time : O(n)
space : O(1)
*/
class Solution {
public void wiggleSort(int[] nums) {
int median = findKthLargest(nums, (nums.length + 1) / 2);
int n = nums.length;
int left = 0, right = n - 1;
int index = 0;
while (index <= right) {
if (nums[newIndex(index, n)] > median) {
swap(nums, newIndex(left++, n), newIndex(index++, n));
} else if (nums[newIndex(index, n)] < median) {
swap(nums, newIndex(right--, n), newIndex(index, n));
} else {
index++;
}
}
}
private int newIndex(int index, int n) {
return (1 + 2 * index) % (n | 1);
}
public int findKthLargest(int[] nums, int k) {
if (nums == null || nums.length == 0) return 0;
int left = 0;
int right = nums.length - 1;
while (true) {
int pos = partition(nums, left, right);
if (pos + 1 == k) {
return nums[pos];
} else if (pos + 1 > k) {
right = pos - 1;
} else {
left = pos + 1;
}
}
}
private int partition(int[] nums, int left, int right) {
int pivot = nums[left];
int l = left + 1;
int r = right;
while (l <= r) {
if (nums[l] < pivot && nums[r] > pivot) {
swap(nums, l++, r--);
}
if (nums[l] >= pivot) l++;
if (nums[r] <= pivot) r--;
}
swap(nums, left, r);
return r;
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
Date:2018年3月8日