Next Permutation
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,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
题目简要:题目涉及到的是全排列问题,全排列的方法有很多,每种全排列的方法得到的排列顺序不同,但是对于一种全排列顺序而言,下一个排列指的是我们所用的排列方法得到一个排列顺序,指定一个排列,它在得到的排列顺序中的下一个排列是什么?本题目是按字典法进行的。
如1,2,3进行全排列为:
1,2,3
1,3,2
2,1,3
2,3,1
3,1,2
3,2,1
这样我们指定一个排列为1,2,3,那么它的下一个排列就是1,3,2.
具体做法:以5,4,7,5,3,2为例,
5,4,7,5,3,2是一个排列,并且我们知道一个排列是有规律的,一个排列是有规律的,一个排列可以分为两个部分(其中一个部分可以为空),其中部分一定是递减顺序的,而另一个也是递减顺序的。5,4是递减的,7,5,4,3是递增的。
要想得到下一个排列,首先我们必须知道将要变动的值是那个。全排的最后一个序列一定是递减顺序的,所以排列两部分中递减顺序的哪部分一定不需要改变,所以我们首先要找到id一个非递减顺序的值,也就是4位置为1,然后找到要和4交换的值,这个值是要在4之后的那个值中查找,要求是大于4的最小的那个,又因为我们是从右向做的查找,所以第一个大于4的即可。交换位置后,对位置1后面的序列进行排序。排序的原因是由字典排序的特性,如1,2,3时,当第一值变为2的时候为2,1,3, 2 后面的序列一定是从小到大的排序的,所以当我们交换两个值后要进行排序。又因为这个算法不看排序方法的时候时间复杂度为O(n),所以这个算法的时间复杂度取决于你的排序算法的时间复杂度。
void Sort(int* arr,int low ,int high)
{
if(low>=high)return ;
int val=arr[low];
int i=low;
int j=high;
while(i<j){
while(i<j&&arr[j]>val)j--;
arr[i]=arr[j];
while(i<j&&arr[i]<=val)i++;
arr[j]=arr[i];
}
arr[i]=val;
Sort(arr,low,i-);
Sort(arr,i+,high);
}
void nextPermutation(int* nums, int numsSize) {
if(numsSize<=)return ;
int i=numsSize-;
while(i>)
{
if(nums[i]>nums[i-])break;
i--;
}
printf("%d\n",nums[i-]);
if(i<=)Sort(nums,,numsSize-);
else{ for(int j=numsSize-;j>=i;j--)
if(nums[i-]<nums[j]){
nums[i-]+=nums[j];
nums[j]=nums[i-]-nums[j];
nums[i-]-=nums[j];
Sort(nums,i,numsSize-);
break ;
}
}
}
Next Permutation的更多相关文章
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- Permutation test: p, CI, CI of P 置换检验相关统计量的计算
For research purpose, I've read a lot materials on permutation test issue. Here is a summary. Should ...
- Permutation
(M) Permutations (M) Permutations II (M) Permutation Sequence (M) Palindrome Permutation II
随机推荐
- 一次线上http接口调用不通相关的解决过程
2016-05-25 08:58:34 昨天线上小白系统因为调用外部http接口,超时不释放,导致页面反应很慢,时间一长,报502错误. 上网查了下,502错误是因为服务对于客户的请求没有得到及时的反 ...
- jQueryNotes仿QQ空间添加标记
jquery-notes有以下特点: 支持添加备注图像 丰富的API 支持标记伸缩 支持更改主题 支持图片标记添加链接 不需要数据库 HTML 首先在页面上放置一张添加标志的图片 <div cl ...
- Amoeba for MySQL读写分离配置
一. 安装和运行Amoeba 1. Amoeba for MySQL 架构: 2. 验证Java的安装Amoeba框架是基于Java SE1.5开发的,建议使用Java SE1.5以上的版本 ...
- SparkMLlib之 logistic regression源码分析
最近在研究机器学习,使用的工具是spark,本文是针对spar最新的源码Spark1.6.0的MLlib中的logistic regression, linear regression进行源码分析,其 ...
- matlab初学之strcat、num2str
文章出处: http://blog.sina.com.cn/s/blog_6fb8aa0d01019id5.html http://wenda.so.com/q/1439143662729624 ht ...
- offse
关于offset共有5个东西需要弄清楚: 1.offsetParent 2.offsetTop 3.offsetLeft 4.offsetWidth 5.offsetHeight (1)offsetW ...
- 4 多表代替密码之Hill 密码 2实现
该解密方法的KEY 不是一个数或者一段字符串,而是一个矩阵, 比如有个3*3的KEY: 那么如果我们要加密一个长度为N的字符串, 那么把N除以3,分成M个3个字母组成的小段, 对每个小段尽心加密: 1 ...
- echo, print, print_r
echo 不是函数,没有返回值,因此只是用作输出的话会更快 print 和 print_r 有返回值,区别在于: print 用于打印一个字符串,print_r 可以打印一些复合类型,如: $arr= ...
- c++构造函数 对象初始化
最近查看了关于c++构造函数的博客,为了防止关键知识的遗忘,特此记录一些要点,以便于今后的查阅. 如果不主动书写构造函数,c++或默认提供一般构造函数,拷贝构造函数以及复制运算符的操作.一般的构造函数 ...
- web页面放到手机页面,缩放问题
有时候写页面样式不规范,很多页面元素写死尺寸时,web页面尺寸比较大放到移动端访问时,就背缩放了,div或者按钮变得好小 可以加段js,效果会好点 <script> ! function( ...