[LeetCode] Wiggle Sort
2015-09-10 12:57
155 查看
Problem Description:
Given an unsorted array
For example, given
The final sorted
If
If
The code is just to fix the orderings of
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].
The final sorted
numsneeds to satisfy two conditions:
If
iis odd, then
nums[i] >= nums[i - 1];
If
iis even, then
nums[i] <= nums[i - 1].
The code is just to fix the orderings of
numsthat do not satisfy 1 and 2.
class Solution { public: void wiggleSort(vector<int>& nums) { int n = nums.size(); for (int i = 1; i < n; i++) if (((i & 1) && nums[i] < nums[i - 1]) || (!(i & 1) && nums[i] > nums[i - 1])) swap(nums[i], nums[i - 1]); } };
相关文章推荐
- 主键生成策略
- Unity-Shader Sublime 自制shader提示插件
- 冒泡排序算法原理及JAVA实现
- java3
- Android(java)学习笔记222:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例的优化:请求码和结果码)
- 检查ip是否在使用
- 0014 sql 基础(1)
- c语言学习二
- UI一揽子计划 13 (UITabBarController 、UITabBar、UITabBarItem 、Block传值)
- oracle用户创建及权限设置
- fs寄存器相关,PEB,TEB
- UI一揽子计划 12 (模态ViewController、单例、通讯录实战、)
- 个推和极光推送技术介绍
- UI一揽子计划 11 (自定义UITableViewCell、Cell 的自适应高度)
- Js读取json数据 && 循环读取JSON数据
- tomcat请求修改为nio处理的配置说明
- UI一揽子计划 10 (UITableView 中cell 的编辑, 增加, 删除、UITableViewController 、)
- UI一揽子计划 9 (UITableView 、UITableView 、重用机制)
- SELECT IN ORDER BY CHARINDEX (排序)
- 同步与异步访问网络