[LeetCode#280] Wiggle Sort

Problem:

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]....

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

Analysis:

Once you encounter a new problem, you should not feel anxious with the new setting.
Try to conclude a principle underlying it, you could find out it is extraordinarily easy.
---------------------------------------------------------
nums[0] <= nums[1] >= nums[2] <= nums[3]....
---------------------------------------------------------
For the wiggle sort, we actually have no requirments over an element‘s global order.
for an element with odd index, we just need to meet following order principle:
nums[i-1] <= nums[i] >= nums[i+1]
Note: i is an odd number. 

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

The above solution is easy. We first guarantee the elements‘ order befor and after nums[i].
But it needs to sort the nums array first, which we could totaly abandon.
Since we only care about an odd(indexed) element‘s realtive order with it neighboring elements, we all just do it all the way.
for (int i = 1; i < nums.length; i++) {
    if (i % 2 == 1) {
        if (nums[i-1] > nums[i])
            swap(nums, i, i-1);
    } else{
        if (nums[i] > nums[i-1])
            swap(nums, i, i-1);
    }
}

The invariant:
we guaratee this is no violation of wiggleSort when we reach i.
Note the start of the for loop.
for (int i = 1; i < nums.length; i++)

Solution:

public class Solution {
    public void wiggleSort(int[] nums) {
        if (nums == null || nums.length <= 0)
            return;
        for (int i = 1; i < nums.length; i++) {
            if (i % 2 == 1) {
                if (nums[i-1] > nums[i])
                    swap(nums, i, i-1);
            } else{
                if (nums[i] > nums[i-1])
                    swap(nums, i, i-1);
            }
        }
    }

    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}
时间: 2024-10-12 04:03:03

[LeetCode#280] Wiggle Sort的相关文章

LeetCode 280. Wiggle Sort (摆动排序)$

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. 题目标签:Array, Sort 题目给了我们一个nums array, 让我们wiggle sort.

LeetCode 280. Wiggle Sort C#

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. Solution: Loop through, when odd index num should be

280. Wiggle Sort/324. Wiggle Sort II

280 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... Example: Input: nums = [3,5,2,1,6,4] Output: One possible answer is [3,5,1,6,2,4] 280要求的是可以大于等于,其实简单. 方法一: 1. 先排序,得到序列 a0<=a1<=a2&l

leetcode [324]Wiggle Sort II

Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... Example 1: Input: nums = [1, 5, 1, 1, 6, 4] Output: One possible answer is [1, 4, 1, 5, 1, 6]. Example 2: Input: nums = [1, 3, 2, 2, 3, 1] Output: One po

280. Wiggle Sort

Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3].... For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. 此题需要把数组序号分成偶数和奇数来区别对待,代码如下: p.p1 { margin: 0.0px 0.0

leetcode 324 Wiggle Sort 2

利用中位数的概念,中位数就是将一组数分成2等份(若为奇数,则中位数既不属于左也不属于右,所以是2等份),其一组数中任何一个元素都大于等于另一组数 那么我们是不是只要一左一右配合着插入,就保证了差值+-+-+-的要求? 由于题目输入限制了,必定存在解,所以此处我们不需要担心如果重复中位数太多,出现一左一右相等的情况 算法步骤: 1)找到中位数 利用lc215 Kth Largest...,将k=(nums.length+1) / 2,可以在o(n)内求出中位数 2)一左一右插入 0 2 4 ..

leetcode笔记:Wiggle Sort

一. 题目描写叙述 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]- For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4]. 二. 题目分析 题目给出一个未排序的数组,调整元素的大小使其满足nums[0] <=

【LeetCode】排序 sort(共20题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [56]Merge Intervals [57]Insert Interval [75]Sort Colors [147]Insertion Sort List [148]Sort List [164]Maximum Gap [179]Largest Number [242]Valid Anagram [252]Meeting Rooms (2018年11月22日,为

Wiggle Sort II

Wiggle Sort II Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3].... 注意事项 You may assume all input has valid answer. 样例 Given nums = [1, 5, 1, 1, 6, 4], one possible answer is [1, 4, 1, 5, 1, 6]. Given nums