问题:
Given an unsorted array nums
, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]...
.
Example:
(1) Givennums = [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?解决:
① 排序法。先给数组排序,然后再做调整。调整的方法是找到数组的中间的数,相当于把有序数组从中间分成两部分,然后从前半段的末尾取一个,在从后半的末尾取一个,这样保证了第一个数小于第二个数,然后从前半段取倒数第二个,从后半段取倒数第二个,这保证了第二个数大于第三个数,且第三个数小于第四个数,以此类推直至都取完。空间复杂度O(n)。
class Solution { //7ms
public void wiggleSort(int[] nums) { Arrays.sort(nums); int len = nums.length; int[] tmp = new int[len]; int mid = (len - 1) / 2; int index = 0; for (int i = 0;i <= mid;i ++){ tmp[index] = nums[mid - i]; if (index + 1 < len){ tmp[index + 1] = nums[len - 1 - i]; } index += 2; } System.arraycopy(tmp,0,nums,0,len); } }② 可以使用快排。
https://discuss.leetcode.com/topic/41464/step-by-step-explanation-of-index-mapping-in-java