【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 ...
随机推荐
- windows系统添加删除用户命令!
参考:net help usernet help group Net user添加或修改用户帐户或者显示用户帐户信息. 语法net user [UserName [Pa ...
- MMTx使用说明
MMTx(MetaMap Transfer)是美国国家医学图书馆建立的用于文本数据挖掘的一种工具. 下面以Medine格式数据为例介绍使用方法 1.在PubMed数据库检索相关的文献. 2.将数据结果 ...
- 关于COOKIE学习的一二
index.php <?php setcookie("name","dalisng",time()+3600); setcookie("addr ...
- VC++6.0MFC运行的简单流程
背景: 由于下位机做的一些东西,总是需要通过上位机来验证,以及为了直观给客户展示下位机的功能,所以时常需要编写一些简单的APP.今天就以VC++6.0为例,简单的记录下该如何快速的创建一个APP. 正 ...
- informatica通用命令方式启动workflow
#传mapping参数时#调用执行workflow,-sv informatica服务,-d 域 -u 用户,-p 密码 -folder知识库下的workflow所在文件夹,-wait wf_test ...
- Lvs原理
官方文档: http://www.linuxvirtualserver.org/zh/lvs1.html http://www.linuxvirtualserver.org/zh/lvs2.html ...
- 【转】 js怎么区分出点击的是鼠标左键还是右键?
IE 下 onMouseDown 事件有个 events.button 可以返回一个数值,根据数值判断取得用户按了那个鼠标键 events.button==0 默认.没有按任何按钮. events. ...
- iOS开发——高级篇——iOS键盘的相关设置(UITextfield)
一.键盘风格 UIKit框架支持8种风格键盘. typedef enum { UIKeyboardTypeDefault, // 默认键盘:支持所有字符 UIKeyboardTypeASCIICapa ...
- << CocoaPods安装和使用教程 >>github code4app以及cocoachina 苹果官方文档
developer.apple.com 英文搜索各个技术的官方介绍文档, 前提是英文过关 cocoachina ios最新新闻, 信息 code4app上有许多组件 http://www.code4a ...
- Unity3D绑定button监听事件
一.可视化创建及事件绑定 第一步:通过Hierarchy面板创建button,如图 第二步:创建一个脚本名为TestClick,并定义一个名为Click的public方法 ? 1 2 3 4 5 6 ...