[Locked] Wiggle Sort
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].
分析:
序列单调性问题,本题要求在不开额外数组的情况下使得序列呈现增减增减的规律
解法:
遍历数组相邻对,遇到不符合要求的,调换位置
证明:
充分性,数学归纳法,之前的序列满足题目要求,当第k个对呈x规律,若第k+1个对呈y规律,满足要求;若第k+1个对呈x规律,调换位置,则呈y规律,满足要求;
必要性,因为题目只要求给出一个可行解,所以无需具备必要性。
代码:
void wiggleSort(vector<int> &a) {
bool inc = true;
for(int i = ; i < a.size() - ; i++) {
if((a[i + ] < a[i] && inc) || (a[i + ] > a[i] && !inc))
swap(a[i], a[i + ]);
inc = !inc;
}
return;
}
[Locked] Wiggle Sort的更多相关文章
- [LeetCode] Wiggle Sort 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] < ...
- Leetcode 280. 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] < ...
- lintcode:Wiggle Sort II
Wiggle Sort II Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] ...
- lintcode:Wiggle Sort
Wiggle Sort Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= ...
- LeetCode 280. 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]... ...
随机推荐
- ArrayList 类和List<T>泛型类
ArrayList集合类在System.Colletions命名空间下,它其实是一个特殊的数组,它可以动态的添加和删除元素,根据元素的改变自动决定它自身的大小,也可以灵活的插入元素等操作,使用起来要比 ...
- Dell服务器MegaCli命令只返回Exit Code: 0x00问题分析
今天同事给我说一台dell的服务器做了raid后,使用MegaCli看不到raid信息,上去看了一下确实不返回任何raid信息,但是确实机器上做了raid. 这就奇怪了,然后把MegaCli升级到最新 ...
- iOS里面如何同时使用开启ARC的库 和 没有开启 ARC的库,ARC 与非 ARC同时存在的问题
旧工程配置arc方案: 1,直接在targets->build phases中修改compiler Flags,是否支持arc.添加:-fobjc-arc,就可以让旧项目支持arc. 新工程配置 ...
- CSS布局模型思考
flow模型:默认布局模型,元素从左向右.从上到下依次排列,块状元素独占一行.Position属性对应值static. float模型:主要效果是让本来独占一行的块状元素变成内联-块状元素,并到一排显 ...
- gulp安装
1. npm install gulp -g 全局安装 npm install gulp --save-dev 安装文件内,纪录于package.json 接著安装插件,完成下列任务 ...
- 【POJ2185】【KMP + HASH】Milking Grid
Description Every morning when they are milked, the Farmer John's cows form a rectangular grid that ...
- 【POJ2887】【块状链表】Big String
Description You are given a string and supposed to do some string manipulations. Input The first lin ...
- 3 - testng.xml
TestNG的调用有以下几种方式: testng.xml ant 命令行 这部分主要介绍testng.xml的格式. 当前testng.xml的DTD(文档类型定义(Document Type Def ...
- c++ undefined reference to mysqlinit
Solved g++ $(mysql_config --cflags) file.cpp -o filename $(mysql_config --libs)
- js四舍五入的bug和方法
简单来说js使用原生toFixed(x)截取小数的时候会有误差,出现在比如var o = 0.3303;o.toFixed(3);//0.330 toFixed(x)本来也是一个获取四舍五入的截取方法 ...