lintcode:Wiggle Sort
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的更多相关文章
- lintcode:Wiggle Sort II
Wiggle Sort II Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] ...
- LintCode 508: Wiggle Sort
LintCode 508: Wiggle Sort 题目描述 给你一个没有排序的数组,请将原数组就地重新排列满足如下性质 nums[0] <= nums[1] >= nums[2] < ...
- leetcode笔记:Wiggle Sort
一. 题目描写叙述 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nu ...
- [LeetCode] Wiggle Sort 摆动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [LintCode] Wiggle Sort II 扭动排序之二
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- [LintCode] Wiggle Sort 扭动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [LeetCode] Wiggle Sort II 摆动排序
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- Leetcode 280. Wiggle Sort
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [Locked] Wiggle Sort
Wiggle Sort Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= ...
随机推荐
- [转]"error while loading shared libraries: xxx.so.x" 错误的原因和解决办法
[转]"error while loading shared libraries: xxx.so.x" 错误的原因和解决办法 http://blog.csdn.net/sahuso ...
- 快速生成R语言报告(markdown+Rstudio)
先预览一下用Markdown写的报告[http://rpubs.com/loness/167347],这是HTML格式,你也可以导出Word.pdf,但是导出pdf时文中不能有中文,但是可以使用“pd ...
- 64位Windows2003下如何正确发布VesnData.Net(VDN)
64位windows2003下发布VDN,按照正常的步骤会出现:试图加载格式不正确的程序. (异常来自 HRESULT:0x8007000B)的错误. 按照下面的步骤进行处理: 1.如果安装了64位F ...
- spring mvc官网下最新jar搭建框架-静态资源访问处理-注解-自动扫描
1.从官网下载spring相关jar http://spring.io/projects 点击SPRING FRAMEWORK
- sqlserver复杂排序(order by case when)
/*表 sysid自增主键 scro分数 oper操作时间 scro分数 > 5的按照 分数 降序, 分数小于等于5的按照 操作时间 升序; >5 的排在 <=5的前面*/ ...
- js之内置对象
内置对象(Global和Math):JS程序在执行之前就已经存在,开发人员不必再取实例化的内置对象 下面对Global对象进行介绍一下,Math用的不多就不做介绍了 1.Global对象 Global ...
- 第一个完整的cppunit单元测试程序
在极限编程中,测试程序本应该在编写主程序之前就要写好,然后将写好的类程序放在测试程序中进行测试,但考虑到项目中需求文档等并未将接口定义好,我无从开始,而且,自己对单元测试也是刚刚熟悉,需要一边写测试程 ...
- 二、freemarker.controller半自动静态化+Tomcat虚拟资源映射
描述:本内容主要是讲2个tomcat之间同时共享一个静态话页面,统一入口是springMVC的一个controller,静态化的更新只需要传false.true.把完成的web项目放入a.b服务器To ...
- TF-IDF与余弦相似性的应用(一):自动提取关键词
这个标题看上去好像很复杂,其实我要谈的是一个很简单的问题. 有一篇很长的文章,我要用计算机提取它的关键词(Automatic Keyphrase extraction),完全不加以人工干预,请问怎样才 ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...