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
随机推荐
- STM32F之IAR6.5 J-Link程序下载错误
错误01:Keil环境下使用J-Link SW模式下载程序,而IAR6.5则出现如图1-1的错误.
- 《嫌疑人X的献身》书评
<嫌疑人X的献身>是日本著名推理小说作家东野圭吾的代表作之一.1985年东野圭吾以一本<放学后>出道,出道初期善于写精巧细致的本格推理,后期文笔愈发老辣.简练.2005年东野圭 ...
- get方式提交中文乱码解决
get方式提交中文时会乱码,过滤器只过滤post请求,此时可修改tomcat配置文件server.xml,为Connector添加属性URIEncoding="utf-8". ec ...
- Python基础知识之认识字符串
Python有一个名为“STR”与许多方便的功能(有一个名为“串”,你不应该使用旧的模块),内置的字符串类. 字符串常量可以通过双或单引号括起来,尽管单引号更常用. 反斜杠工作单,双引号内的文字通常的 ...
- 第三篇——软件之殇,WE ARE THOUSANDS APART!
软件工程是一门工程性的学科,其目标主要是成功地建造一个大型软件系统.这其中包括:付出较低的开发成本:达到要求的软件功能:取得较好的软件性能:开发的软件易于移植:需要较低的维护费用:能按时完成开发任务, ...
- iOS 上拉下拉刷新简单实现代码
一般说到上拉刷新下拉刷新,很多人可能想到的是一个第三方开源框架EGORefresh,下面说下,如何自己写代码实现. UITableView本身是一个UIScrollView,所以UITableView ...
- unix下输出重定向
> 为重定向符号 >> 重定向不覆盖原文件内容 example: 1. 标准输出重定向 echo "123" > /home/123.txt ---- 标准 ...
- docker 源码分析 五(基于1.8.2版本),Docker容器的创建
前面讲到了docker容器得镜像,镜像其实是docker容器的静态部分,而docker容器则是docker镜像的动态部分,即启动了一个进程来运行,本篇最要来分析一下怎样创建并运行一个容器. 创建一个容 ...
- Integer 中的缓存类IntegerCache
2014年去某公司笔试的时候遇到这么一道题: public class Test { public static void main(String[] args) { Integer int1 = I ...
- ios网络请求特殊字符&处理
原文地址:http://www.xuebuyuan.com/2039420.html CFURLCreateStringByAddingPercentEscapes 在作项目的的时候,一般都要用到网络 ...