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
随机推荐
- JAVA中SERIALVERSIONUID的解释
serialVersionUID作用: 序列化时为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性.有两种生成方式: 一个是默认的1L,比如:private st ...
- cookie怎么用
cookie是什么? cookie是浏览器提供的一种机制,它将document 对象的cookie属性提供给JavaScript.可以由JavaScript对其进行控制,而并不是JavaScript本 ...
- Windows进程崩溃问题定位方法
Linux上进程崩溃通常会生成core文件,用gdb打开后执行bt命令即可查看堆栈.而在Windows平台上,我们通常会采用MiniDumpWriteDump来进行堆栈转储,而这需要对系统Api有一定 ...
- 持续集成配置-Teamcity
1.安装目录\buildagent\work\每个项目下一个文件夹\src下是源 目的: 2. 安装目录\buildagent\work\每个项目下一个文件夹\src下是源 目的是teamcity中配 ...
- MVC 实体字段自定义验证
[Remote("ActionName", "ControllerName", AdditionalFields = "ID", Error ...
- 转:AJAX中xhr对象详解
XJAX ,并不是一种新技术的诞生.它实际上代表的是几项技术按一定的方式组合在一在同共的协作中发挥各自的作用. 它包括: 使用XHTML和CSS标准化呈现: 使用DOM实现动态显示和交互: 使用XML ...
- Spark源码学习1.4——MapOutputTracker.scala
相关类:MapOutputTrackerMessage,GetMapOutputStatuses extends MapPutputTrackerMessage,StopMapOutputTracke ...
- PAT 5-8 File Transfer (25分)
We have a network of computers and a list of bi-directional connections. Each of these connections a ...
- centos系列——1
今天开始好好学一下centos,下面记录全过程,以方便后来查阅. 开机后用户名为root. 忘记密码修改方法: 以GRUB 多系统引导程序启动,用GRUB引导系统进入单用户步骤:(1) 启动GRUB, ...
- wordpress模板学习之导航目录
wordpress的导航目录分为三个部分,一开启,二配置:三使用 开启在functions.php,这个注册会保存在全局变量中,接下来在菜单配置中会看到 register_nav_menu( 'pri ...