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] >= ...
随机推荐
- IOS判断网络环境
https://developer.apple.com/library/ios/#samplecode/Reachability/Introduction/Intro.html 我下载的是vertio ...
- [转]AngularJS: 使用Scope时的6个陷阱
在使用AngularJS中的scope时,会有6个主要陷阱.如果你理解AngularJS背后的概念的话,这6个点其实非常的简单.但是在具体讲述这6个陷阱之前我们先要讲两个其它的概念. 概念1: 双向数 ...
- Java实现Socket之TimeClient
Java实现Socket之TimeClient 代码内容 从time.nist.gov服务器的37号端口得到时间信息,并对时间进行解析后显示出来 代码实现 /* TimeClient.java */ ...
- FPGA控制HC595
/*****************************************************************************Copyright: 2013File na ...
- 转Oracle字符集问题总结
Oracle字符集问题总结 分类: Oracle2006-06-04 13:48 1298人阅读 评论(3) 收藏 举报 oracle数据库sqlcharacter存储insert 作者: vston ...
- 查找bad sql的方法:
--查找bad sql的方法: select * from (select buffer_gets, sql_text from v$sqlarea ; -- 执行次数多的SQL select sql ...
- 最基础的Hash
type thash=^node; node=record state:longint; next:thash; end; var a,i:longint; p:thash; hash:..]of t ...
- Xcode 添加代码块
我们经常会定义一些retain的property,而且大概每次我们都会像这样写: @property (nonatomic, retain) Type *name; 每次都要老老实实的把“@prop ...
- 使用Log Explorer查看和恢复数据
由于一次意外操作,把QC数据库中的BUG表数据给删掉了.崩溃-上网找了下恢复方法,找到一款Log Explorer.下载安装使用后,发现这款软件的确不错,收藏ing. 本次的使用的Log Expl ...
- 三张图看遍Linux 性能监控、测试、优化工具
Linux 平台上的性能工具有很多,眼花缭乱,长期的摸索和经验发现最好用的还是那些久经考验的.简单的小工具.系统性能专家 Brendan D. Gregg 在最近的 LinuxCon NA 2014 ...