【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.1,2,3 → 1,3,23,2,1 → 1,2,31,1,5 → 1,5,1
思路:
首先,从后向前找到第一对递增的的数 如 1 4 2 5 7 6 3 中的 5 7, 这表示,7 6 3 是一个连续递减的序列,即这三个数字可以组成的最大的数。 把这三个数翻转,就是 3 6 7即这三个数字可以组成的最小的数字。把5与这三个数字中比它大的最小的数字与它交换变成 1 4 2 6 7 5 3,即下一个较大的数字。注意,像2 3 1 3 3 这样后面比交换点数大一点的数有相同的时候,后面翻转后,交换后面的那个。
class Solution {
public:
void nextPermutation(vector<int> &num)
{
if (num.empty()) return;
// in reverse order, find the first number which is in increasing trend (we call it violated number here)
int i;
for (i = num.size()-; i >= ; --i)
{
if (num[i] < num[i+]) break;
}
// reverse all the numbers after violated number
reverse(begin(num)+i+, end(num));
// if violated number not found, because we have reversed the whole array, then we are done!
if (i == -) return;
// else binary search find the first number larger than the violated number
auto itr = upper_bound(begin(num)+i+, end(num), num[i]);
// swap them, done!
swap(num[i], *itr);
}
};
我自己写得:思路是先交换,再翻转。比上面的繁琐一点。
void nextPermutation(vector<int> &num) {
if(num.empty()) return;
bool b = false;
int chg1 = , chg2 = ;
int post = num.size() - ;
for(int i = num.size() - ; i >= ; --i)
{
if(num[post] > num[i])
{
b = true;
chg1 = i;
chg2 = post;
break;
}
post = i;
}
if(!b)
{
reverse(num.begin(), num.end());
return;
}
for(int i = chg1 + ; i < num.size(); i++)
{
if(num[i] > num[chg1] && num[i] <= num[chg2])
chg2 = i;
}
swap(num[chg1], num[chg2]);
int l1 = chg1 + ;
int l2 = num.size() - ;
while(l1 < l2)
{
swap(num[l1++], num[l2--]);
}
return;
}
【leetcode】Next Permutation(middle)的更多相关文章
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reorder List (middle)
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do thi ...
- 【leetcode】Word Break (middle)
Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa ...
- 【leetcode】Rotate List(middle)
Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...
- 【leetcode】Partition List(middle)
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- 【leetcode】Spiral Matrix(middle)
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...
- 【leetcode】Rotate Image(middle)
You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwise). ...
- 【leetcode】Reverse Bits(middle)
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...
- 【leetcode】Surrounded Regions(middle)☆
Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captured ...
随机推荐
- UnicodeDecodeError: 'utf8' codec can't decode
数据库存了些中文字符, 比如'处理脚本'这样的汉字, 结果导致一个python程序报错. 下面记录处理过程和相关结论. ===========================dal.py 程序片段,p ...
- EF更新指定字段...
EF更新指定的字段... 搜来搜去发现没有自己想要的啊... 或许本来就有更好的办法来实现我这个,所以没有人来搞吧... 如果有,请不吝告知..GG.. //要更改UserInfo表中指定的列,比如这 ...
- 找不到类型“{x}.{x}”,它在 ServiceHost 指令中提供为 Service 特性值,或在配置元素 system.serviceModel/serviceHostingEnvironment/serviceActivations 中提供。
最近在搞一个WCF的项目... 刚开始在这条路上走... 各种崎岖... 网上搜到的一种解决方案(也是大多数情况的解决方案): 原文:http://www.cnblogs.com/Olive116/p ...
- 微信电话本可免费拨打网络电话 通话一分钟约300K流量
微信电话本新版本于昨日晚间发布,这是一款智能通讯增强软件,通话双方都下载此APP并开通免费通话功能就能使用微信电话本拨打免费网络电话,在对方无法接通情况下还能将音频转向语音信箱,微信电话本目前支持An ...
- 在WordPress后台菜单系统中添加Home链接
在wordpress后台如果想打开前台的话,要想先把鼠标移动到左上角菜单,然后在下拉菜单中点击“查看站点”,很是麻烦,能不能在 WordPress 后台菜单系统中添加 Home 链接呢? 将下面代码复 ...
- 【C语言入门教程】4.5 指针变量的定义与引用
指针变量是包含内存地址的变量.一般的变量直接包含一个特定的值,而指针变量包含的是某一特定数据类型的内存地址.普通变量直接引用其中的值,指针变量则间接引用所指向内存地址中的值.指针变量在使用前需要声明与 ...
- 改造 ThinkPHP,弃用 D() 等魔术函数
ThinkPHP 是国内比较优秀的 PHP 框架,但有些地方不是很好,比如那些 魔术函数 D(),用它返回的类实例,在各个IDE(如 PhpStorm)下根本识别不了,导致如下问题: 1.不支持 代码 ...
- Swift2.1 语法指南——错误处理
原档:https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programmi ...
- iOS resign code with App Store profile and post to AppStore
http://stackoverflow.com/questions/17545452/ios-resign-code-with-app-store-profile-and-post-to-appst ...
- Apache开发模块
输入perl Configure.pl 安装目录...\apache2.2 “httpd.exe” 生成apxs命令, apache2.2下build目录中的config_vars.mk文件 将CC ...