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的更多相关文章

  1. [LeetCode] Wiggle Sort II 摆动排序

    Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...

  2. [LeetCode] Wiggle Sort 摆动排序

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  3. Leetcode 280. Wiggle Sort

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  4. [LintCode] Wiggle Sort II 扭动排序之二

    Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...

  5. [LintCode] Wiggle Sort 扭动排序

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  6. lintcode:Wiggle Sort II

    Wiggle Sort II Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] ...

  7. lintcode:Wiggle Sort

    Wiggle Sort Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= ...

  8. LeetCode 280. Wiggle Sort (摆动排序)$

    Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] < ...

  9. [LeetCode] Wiggle Sort II 摆动排序之二

    Given an unsorted array nums, reorder it such that nums[0] < nums[1] > nums[2] < nums[3]... ...

随机推荐

  1. SQL替换空格,制表符,换行符,回车符.

    首先是空格的替换,很重要的有点是,要确保字段的类型,不是char或nchar等固定的类型,否则无法去掉空格. 去掉空格很简单,如下为SQL实例: --去掉 T_StuffBasic 表中FBranch ...

  2. javascript基础学习(四)

    javascript之流程控制语句 学习要点: 表达式语句含义 选择语句:if.if...else.switch 循环语句:while.do...while.for.for...in 跳转语句:bre ...

  3. Vijos1834 NOI2005 瑰丽华尔兹 动态规划 单调双端队列优化

    设dp[t][x][y]表示处理完前t个时间段,钢琴停留在(x,y)处,最多可以走多少个格子 转移时只需逆着当前倾斜的方向统计len个格子(len为时间区间的长度,len=t-s+1),如果遇到障碍就 ...

  4. SGU 113.Nearly prime numbers

    水一个代码: #include <iostream> using namespace std; int n, a; bool ok; bool prime (int x) { ; i * ...

  5. 怎么去掉iframe的滚动条?

    <iframe name="123" src="YNJD/ynjd.htm" frameborder="0" width=" ...

  6. win7下.NET 2.0未在web服务器上注册的问题(转)

    转自:http://blog.sina.com.cn/s/blog_6d15b547010192hx.html 电脑装了win7操作系统,装上vs2008后运行dotnetnuke项目后出现" ...

  7. 用arm-linux-gcc v4.3.4交叉编译Qt4.8.3

    1.解压缩 #tar zxvf  qt-everywhere-opensource-src-4.8.3.tar.gz 2. configure #mkdir buildarm-static #cd b ...

  8. java 下载文件 内容为空。

    检查下是不是io流没有关闭,记得关闭所有流.

  9. wpf样式绑定 行为绑定 事件关联 路由事件实例

    代码说明:我要实现一个这样的功能  有三个window窗口  每个窗体有一个label标签  当我修改三个label标签中任意一个字体颜色的时候  其他的label标签字体颜色也变化 首先三个窗体不用 ...

  10. 关于call 与 apply 那些事

    1.定义: call : 调用一个对象的一个方法,以另一个对象替换当前的对象. apply : 应用某一对象的一个方法,用另一个对象替换当前的对象. 2.用法: call:call(thisObj, ...