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] >= ...
随机推荐
- Swift的一些基础内容
//①判断字符串是否为空的方法 isEmpty var str:String = "www.baidu.com" if str.isEmpty { print("空字符串 ...
- 兼容sdk7&iOS7的issue解决小片段总结
ios7新增加的icon尺寸: 76 x 76:Size for iPad 2 and iPad mini (standard resolution) 120 x 120 :Size for iPho ...
- find用法
find - 递归地在层次目录中处理文件 总 find [path...] [expression] 描 这个文档是GNU版本 find 命令的使用手册. find 搜索目录树上的每一个文件名,它从左 ...
- [shell基础]——read命令
read命令:在shell中主要用于读取输入.变量.文本 1. 接受标准输入(键盘)的输入,并将输入的数据赋值给设置的变量 [按回车键——表示输入完毕] [若输入的数据多于设置的变 ...
- java笔试题(3)
short a = 1; a = a + 1; 有错吗? short a = 1; a += 1; 有错吗? 对于short a = 1; a = a + 1;由于a + 1 运算时会自动提升表达式的 ...
- 随笔 planetest
Camera跟随物体: import Scripts包,Component中的camera control会有smooth follow脚本,添加到Main Camera中,在脚本的target属性中 ...
- asp.net页面的请求处理响应的过程描述
概述 本篇博客从IIS到asp.net页面后台运行完,整个过程做一个简单的描述,如果有不对的地方,望指出. IIS处理请求的过程 我们通过浏览器(Socket客户端)访问一个IIS服务器上的网页时,该 ...
- 学习Linux第五天
1.VIM编辑器 3种模式: Command Model , Insert Model , Last line Model 安装vim: sudo apt-get install vim 如果提示出错 ...
- 显示器VGA视频数据线的问题
一朋友原来有一套PC电脑,后来买了一台新的显示器,新的显示器分辨率为1920X1080,主机接到新的显示器上,分辨率始终无法上到1920X1080,原主机的显示卡驱动已经是最新,还是不行,又重新安装操 ...
- 对git认识
Github则是一个基于Git的日益流行的开源项目托管库.它的使用流程不需要联机,可以先将对代码的修改,评论,保存在本机.等上网之后,再实时推送过去.同时它创建分支与合并分支更容易,推送速度也更快,配 ...