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. ListView usage in ERP-DEV

    ListView Learning Note how to add double click event to listviewitem in ListView. refer link in stac ...

  2. Content Template & DataTemplate 区别

    转一篇很好的博客: http://www.cnblogs.com/lzhp/p/3250786.html 介绍 listbox 的 Template.ItemsPanel.ItemContainerS ...

  3. licens 问题 Error (292028): Specified license is not valid for this machine

    集成网卡调试的时候坏了,造成了quartus 不可以用,MAC地址不对应了... 应该怎么解决呢??.

  4. chmod修改文件权限的命令

    语法: chmod [options] mode files options: -c,--changes只输出被改变文件的信息-f,--silent,--quiet当chmod不能改变文件模式时,不通 ...

  5. 【BZOJ 2324】 [ZJOI2011]营救皮卡丘

    Description 皮卡丘被火箭队用邪恶的计谋抢走了!这三个坏家伙还给小智留下了赤果果的挑衅!为了皮卡丘,也为了正义,小智和他的朋友们义不容辞的踏上了营救皮卡丘的道路. 火箭队一共有N个据点,据点 ...

  6. Ionic 2 Guide

    Ionic 2 Guide 最近一直没更新博客,业余时间都在翻译Ionic2的文档.之前本来是想写一个入门,后来觉得干脆把官方文档翻译一下算了,因为官方文档就是最好的入门教程.后来越翻译越觉得这个事情 ...

  7. android ViewPaper高度自适应

    tv_btn_web.measure(0, 0);//计算所需的真实宽高 LayoutParams params=vp_btn_menu.getLayoutParams(); params.heigh ...

  8. 【Binary Tree Preorder Traversal】cpp

    题目: Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binar ...

  9. php图形图像处理之生成验证码

    \(^o^)/~ 现在网上越来越离不开验证码了,不知道小伙伴们知不知利用php的GD库就可以生成验证码,Σ(⊙▽⊙"a ...... 首先介绍几个需要用的函数. 1.imagesetpixe ...

  10. 基于ArcEngine的空间数据通用建库软件介绍

    最近花了点时间把之前的空间数据入库功能进行了完善,在这里做一个简单的介绍,也希望大家给提点意见和建议,我的目标是做一个好用.易用.通用.稳定的入库程序. 1.软件特点: 基于模板(方案)的数据更新   ...