zoukankan      html  css  js  c++  java
  • 【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].

    解析

    摇摆排序
    只要奇数位上的数比左右偶数位上的数都大即可

    思路

    贪心算法:在对问题求解时,总是做出在当前看来是最好的选择。
    也就是说,不从整体最优上加以考虑,他所做出的是在某种意义上的局部最优解。

    解法

        public int[] wiggleSort(int[] nums) {
            for (int i = 1; i < nums.length; i++) {
                if ((((i & 1) == 1) && (nums[i] < nums[i - 1])) || (((i & 1) == 0) && (nums[i] > nums[i - 1]))) {
                    int temp = nums[i];
                    nums[i] = nums[i - 1];
                    nums[i - 1] = temp;
                }
            }
            return nums;
        }
    
  • 相关阅读:
    Ubuntu16.04.1 安装Nginx
    Ubuntu16.04.1 安装Redis-Cluster
    SeekBar的简单使用
    Async异步处理
    SQLite
    Shareprefrence
    android中的主线程
    Fragment的简单使用
    ArrayAdapter的使用
    用Intent传递对数
  • 原文地址:https://www.cnblogs.com/shanelau/p/7238096.html
Copyright © 2011-2022 走看看