原题

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;
}

【leetcode】280.Wiggle Sort的更多相关文章

  1. 【LeetCode】280. Wiggle Sort 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序后交换相邻元素 日期 题目地址:https://l ...

  2. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  3. 【LeetCode】147. Insertion Sort List 解题报告(Python)

    [LeetCode]147. Insertion Sort List 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: h ...

  4. 【leetcode】147. Insertion Sort List

    Sort a linked list using insertion sort. 链表的插入排序. 需要创建一个虚拟节点.注意点就是不要节点之间断了. class Solution { public: ...

  5. 【Leetcode】376. Wiggle Subsequence

    Description: A sequence of numbers is called a wiggle sequence if the differences between successive ...

  6. 【leetcode】1122. Relative Sort Array

    题目如下: Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 ar ...

  7. 【LeetCode】791. Custom Sort String 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 按顺序构造字符串 排序 日期 题目地址:https: ...

  8. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  9. leetcode 280.Wiggle Sort 、324. Wiggle Sort II

    Wiggle Sort: 注意:解法一是每次i增加2,题目不是保证3个3个的情况,而是整个数组都要满足要求. 解法一错误版本: 如果nums的长度是4,这种情况下nums[i+1]会越界.但是如果你用 ...

随机推荐

  1. 算法习题---5.2木块问题(UVa101)

    一:题目 输入n,得到编号为0~n-1的木块,分别摆放在顺序排列编号为0~n-1的位置.现对这些木块进行操作,操作分为四种. .move a onto b:把木块a.b上的木块放回各自的原位,再把a放 ...

  2. 判断本网段有多少可用的ip地址

    为了提高效率,使用多线程方式同时ping. 但是如果开启255个线程,又会因为网络端口太拥挤,会被判定为无法ping通.所以本例使用java自带线程池,线程池的连接数还不能太大,启动了15个线程. 等 ...

  3. centos 7 修改计算机名

      [root@centos7 ~]$ hostnamectl set-hostname test.xyz.com # 使用这个命令会立即生效且重启也生效 [root@centos7 ~]$ host ...

  4. Java以UTF-8格式读写及追加写文件示例

    package test; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; im ...

  5. 【Leetcode_easy】897. Increasing Order Search Tree

    problem 897. Increasing Order Search Tree 参考 1. Leetcode_easy_897. Increasing Order Search Tree; 完

  6. Docker快速入门——Docker-Compose

    一.Docker-Compose简介 1.Docker-Compose简介 Docker-Compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排.Docker-Com ...

  7. [LeetCode] 849. Maximize Distance to Closest Person 最大化最近人的距离

    In a row of seats, 1 represents a person sitting in that seat, and 0 represents that the seat is emp ...

  8. C# .NET 判断输入的字符串是否只包含数字和英文字母

    FROM :   https://www.cnblogs.com/ilookbo/p/4828722.html /// <summary> /// 判断输入的字符串是否只包含数字和英文字母 ...

  9. google浏览器插件fq教程

    google插件fq教程 思路 谷歌浏览器的应用市场 上有很多vp恩插件,不过要能访问谷歌浏览器的应用市场就得fq出去才行,而有一个很棒的插件就可以访问谷歌的旗下的一些东西,例如邮箱,Google搜索 ...

  10. 热力图heatmap使用

    参考:https://www.cnblogs.com/julinhuitianxia/p/7755246.html 1.首先到echarts官网下载应用实例:http://echarts.baidu. ...