280. Wiggle Sort
题目:
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] <= nums[3]....
For example, given nums = [3, 5, 2, 1, 6, 4], one possible answer is [1, 6, 2, 5, 3, 4].
链接: http://leetcode.com/problems/wiggle-sort/
题解:
Wiggle排序数组。按照题意写就可以了。 还可以简化,要多学习Stefan的代码。
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public void wiggleSort(int[] nums) {
for(int i = 1; i < nums.length; i++) {
if(i % 2 == 1) {
if(nums[i] < nums[i - 1]) {
swap(nums, i);
}
} else {
if(i != 0 && nums[i] > nums[i - 1]) {
swap(nums, i);
}
}
}
}
private void swap(int[] nums, int i) {
int tmp = nums[i - 1];
nums[i - 1] = nums[i];
nums[i] = tmp;
}
}
二刷:
方法和一刷一样。在i % 2 == 1或者 == 0的时候作交换的判断,交换完毕以后仍然保持这是一个本地化操作就可以了。交换的时候是用 i和 i - 1来交换
Java:
Time Complexity - O(n), Space Complexity - O(1)
public class Solution {
public void wiggleSort(int[] nums) {
if (nums == null || nums.length == 0) {
return;
}
for (int i = 1; i < nums.length; i++) {
if (i % 2 == 1) {
if (nums[i] < nums[i - 1]) {
swap(nums, i);
}
} else {
if (nums[i] > nums[i - 1]) {
swap(nums, i);
}
}
}
}
private void swap(int[] nums, int i) {
int tmp = nums[i - 1];
nums[i - 1] = nums[i];
nums[i] = tmp;
}
}
三刷:
跟前面一样,也是直接编写。
Java:
public class Solution {
public void wiggleSort(int[] nums) {
if (nums == null || nums.length < 2) return;
for (int i = 1; i < nums.length; i++) {
if ((i % 2 == 0 && nums[i] > nums[i - 1]) || (i % 2 == 1 && nums[i] < nums[i - 1])) {
int tmp = nums[i];
nums[i] = nums[i - 1];
nums[i - 1] = tmp;
}
}
}
}
Reference:
https://leetcode.com/discuss/57113/java-o-n-solution
https://leetcode.com/discuss/57206/java-o-n-10-lines-consice-solution
https://leetcode.com/discuss/60824/java-python-o-n-time-o-1-space-solution-3-lines
https://leetcode.com/discuss/57118/easy-code-of-python
280. Wiggle Sort的更多相关文章
- leetcode 280.Wiggle Sort 、324. Wiggle Sort II
Wiggle Sort: 注意:解法一是每次i增加2,题目不是保证3个3个的情况,而是整个数组都要满足要求. 解法一错误版本: 如果nums的长度是4,这种情况下nums[i+1]会越界.但是如果你用 ...
- Leetcode 280. Wiggle Sort
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- LeetCode 280. Wiggle Sort (摆动排序)$
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- [LeetCode] 280. Wiggle Sort 摆动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- 【LeetCode】280. Wiggle Sort 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序后交换相邻元素 日期 题目地址:https://l ...
- LeetCode 280. Wiggle Sort C#
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
- 【leetcode】280.Wiggle Sort
原题 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] & ...
- [LeetCode] 324. Wiggle Sort II 摆动排序 II
Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...
- [LeetCode] Wiggle Sort 摆动排序
Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...
随机推荐
- Notes of the scrum meeting(12.7)
meeting time:18:30~19:10p.m.,December 7th,2013 meeting place:3号公寓一层 attendees: 顾育豪 ...
- winform - BackgroundWorker
http://www.cnblogs.com/happy555/archive/2007/11/07/952315.html 在VS2005中添加了BackgroundWorker组件,该组件在多线程 ...
- wifi current SSID
1. 引入头,#import <SystemConfiguration/CaptiveNetwork.h> 2. 获取SSID info + (id)fetchSSIDInfo { NSA ...
- fiddler 代理不成功调试
按照 http://jingyan.baidu.com/article/03b2f78c7b6bb05ea237aed2.html 设置. 之前可以用, 现在不能用. 1. 关闭 系统防火墙 2. 换 ...
- 无法解决 equal to 运算中 "Chinese_PRC_CI_AS" 和 "SQL_Latin1_General_CP1_CI_AS" 之间的排序规则冲突。
什么是排序规则(collation) 关于SQL Server的排序规则,估计大家都不陌生,在创建数据库时我们经常要选择一种排序规则(conllation),一般我们会留意到每一种语言的排序规则都有许 ...
- 使用Zend OpCache 提高 PHP 5.5+ 性能
使用Zend OpCache 提高 PHP 5.5+ 性能 作者:admin | 时间:February 28, 2015 | 分类:Linux | 评论:1 评论 PHP 5.5 以后内建了 OpC ...
- 初识layer 快速入门
http://layer.layui.com/hello.html 如果,你初识layer,你对她不知所措,你甚至不知如何绑定事件… 那或许你应该用秒做单位,去认识她. 开始了解 第一步:部署 下载l ...
- ASP.NET Excel 导入 Oracle 方法2
先谈思路:前半部分和之前那篇日志的内容是一样的,把Excel数据导入到DataSet中,不同之处在于数据插入的方式: 本方法是拼接 INSERT INTO TABLE VALUES() 字符串,对,就 ...
- -高级Javascript编程学习笔记----Javascript编程及架构设计最应该注意的基本点
最小全局变量 JavaScript通过函数管理作用域.在函数内部生命的变量只在这个函数内部,别的地方不可用.全局变量是指在函数外或是未声明直接简单使用的.每个Javascipt环境有一个全局对象,当你 ...
- POJ 2533 Longest Ordered Subsequence
题目描述:LIS(Longest Increasing Subsequence)模板题 分析:O(n^2)的方法 状态表示:d[i]表示以i结尾的最长上升子序列长度 转移方程:d[i]=max{ 1, ...