[LintCode/LeetCode] Wiggle Sort I & Wiggle Sort II

457 查看

Wiggle Sort

Problem

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

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

Example

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

Note

每隔两位交换一次,如【1 2 3 4 5 6】,处理为【1 3 2 5 4 6】。

Solution

public class Solution {
    public void wiggleSort(int[] nums) {
        Arrays.sort(nums);
        for (int i = 2; i < nums.length; i+=2) {
            int temp = nums[i];
            nums[i] = nums[i-1];
            nums[i-1] = temp;
        }
        return;
    }
}

Wiggle Sort II

Problem

Given an unsorted array nums, reorder it such that

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

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

Note

难点是会有相等的元素,而要求相邻元素除了wiggle外,不能相等。
那么就不能取排序后相邻的元素交换,而要和后面的元素交换。例如:

//1 2 3 4 5 6
//3 6 2 5 1 4

牺牲空间的做法是,建立一个新数组temp,按照我们想要的规律放入元素,最后copy回原数组nums。
简单的思路就是,假设nums里有n个数,我们循环n/2次或者n/2+1次,每次循环里为temp添加两个数(n为奇数时,最后一次循环只添加一个数)。最后用System.arraycopy(sourceArray, 0, targetArray, 0, targetArray.length).

Solution

耗时浪费空间法
public class Solution {
    public void wiggleSort(int[] nums) {
        Arrays.sort(nums);
        int n = nums.length, mid = (n-1)/2, 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-1-i];
            }
            index += 2;
        }
        System.arraycopy(temp, 0, nums, 0, n);
    }
}