zoukankan      html  css  js  c++  java
  • [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;
        }
    }
  • 相关阅读:
    [Swift]LeetCode241. 为运算表达式设计优先级 | Different Ways to Add Parentheses
    [Swift]LeetCode240. 搜索二维矩阵 II | Search a 2D Matrix II
    使用ADO.NET对SQL Server数据库进行訪问
    JavaScript中面向对象那点事
    总结文件操作函数(二)-C语言
    UVa
    深入研究java.lang.Object类
    TCP/IP具体解释--TCP/IP可靠的原理 滑动窗体 拥塞窗体
    W5500EVB UDP模式的測试与理解
    仿新浪首页、主题、详情页,纯html静态页面
  • 原文地址:https://www.cnblogs.com/airwindow/p/4823159.html
Copyright © 2011-2022 走看看