Wiggle Sort

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

nums[0] <= nums[1] >= nums[2] <= nums[3]....
注意事项

Please complete the problem in-place.

样例

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

解题

竟然可以直接交换

对i位置

对奇数位需要是:A[i] >= A[i-1]  奇数位的数大于后一个的数,当是A[i] < A[i-1] 的时候交换

对偶数位需要是:A[i] <=A[i-1] 偶数位的数小于后一个数,当是A[i] > A[i-1] 的时候交换

对给的样例

原始数组:[3,5,2,1,6,4]

第一次:[3,5,2,1,6,4] 3<=5 不交换

第二次:[3,5,2,1,6,4] 5>=2 不交换

第三次:[3,5,1,2,6,4] 2>1 交换 小的换到前面不影响上一次的情况 1<=2

第四次:[3,5,1,6,2,4] 2<6 交换, 大的换的前面不影响,6>=2

第五次:[3,5,2,1,6,4] 2<=4 不需要交换

public class Solution {
/**
* @param nums a list of integer
* @return void
*/
public void wiggleSort(int[] nums) {
// Write your code here
if(nums == null || nums.length == 0)
return;
int n = nums.length;
for(int i = 1;i<n ; i++){
if( (i%2==1 && nums[i] < nums[i-1] ) || (i%2==0 && nums[i] > nums[i-1])){
nums[i]^=nums[i-1];
nums[i-1]^=nums[i];
nums[i]^=nums[i-1];
}
}
}
}

lintcode:Wiggle Sort的更多相关文章

  1. lintcode:Wiggle Sort II

    Wiggle Sort II Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] ...

  2. LintCode 508: Wiggle Sort

    LintCode 508: Wiggle Sort 题目描述 给你一个没有排序的数组,请将原数组就地重新排列满足如下性质 nums[0] <= nums[1] >= nums[2] < ...

  3. leetcode笔记:Wiggle Sort

    一. 题目描写叙述 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nu ...

  4. [LeetCode] Wiggle Sort 摆动排序

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

  5. [LintCode] Wiggle Sort II 扭动排序之二

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

  6. [LintCode] Wiggle Sort 扭动排序

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

  7. [LeetCode] Wiggle Sort II 摆动排序

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

  8. Leetcode 280. Wiggle Sort

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

  9. [Locked] Wiggle Sort

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

随机推荐

  1. MVC4.0 利用HandleErrorAttribute和log4net实现记录异常日志功能

    1.MVC4.0中HandleErrorAttribte已经帮我们处理了异常问题,当我们新建一个非空的MVC项目时候,在FilterConfig中会发现这样的代码 public class Filte ...

  2. SQL Server数据库学习笔记-概念数据模型

    概念数据模型(Conceptual Data Model)也称为信息模型.它是对客观事物及其联系的抽象,用于信息世界的建模,是现实世界到信息世界的第一层抽象,是数据库设计人员进行数据库设计的有力工具. ...

  3. MNC - Multicast NetCat

    MNC - Multicast NetCat 使用nc测试udp多播,总是遇到奇怪的问题,搞的一头雾水.偶然发现了MNC,测试了一下果然好用. 下载地址: https://github.com/mar ...

  4. HTML/CSS的学习过程一览

    HTML/CSS的学习过程一览 说明 调试工具使用的是Google Chrome浏览器,其余浏览器出现的问题,这锅我不背[傲娇脸 可以使用浏览器查看源代码 网页列表 HTML_CSS_1 HTML基本 ...

  5. Mono for Android (1) 之布局

    最近和同事交接工作,首次接触mono for android, 结果画view时少了layout,页面没办法出来,各种冥思,各种找问题,最后把关于布局的一些共享出来(同事写的,哈哈):   Andro ...

  6. angularjs resources

    http://tutorials.jenkov.com/angularjs/watch-digest-apply.html http://angular-tips.com/blog/2013/08/w ...

  7. linux入门基础_centos(一)--基础命令和概念

    闲来无事干,看看2014自己整理的一些学习笔记.独乐了不如众乐乐吗! 贴出来和大家分享一下,由于篇幅比较长,分成几篇发布吧,由于是学习笔记,可能有些地方写的不是很正确或者说不详细,或者你会看到上面的课 ...

  8. 细究UTF-8,GB2312及ISO-8859-1区别

    各个国家和地区所制定的不同 ANSI 编码标准中,都只规定了各自语言所需的“字符”.比如:汉字标准(GB2312)中没有规定韩国语字符怎样存储.这些 ANSI 编码标准所规定的内容包含两层含义:1. ...

  9. android开发 自定义图文混排控件

    功能:图文混排,可自动缩放字体,如图: 单点触控使用的代码来自:http://blog.csdn.net/xiaanming/article/details/42833893  谢谢博主! 在该dem ...

  10. Word中字体背景有白块咋办

      如下图,主要是从新浪博客贴过来的,先用记事本很麻烦. 1. 记事本转帖,麻烦,有公式的话需要单独处理,更麻烦 2.菜单栏中的油漆桶(段落那),有时候不行. 3.粘到QQ对话框再占回来,完美. 4. ...