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
随机推荐
- Activity 生命周期
Activity 的四种基本状态 1.运行态(Running) Activity 处于屏幕最前端,用户可见且获得焦点. 2.暂停态(Paused) Activity被置于后台,用户可见,但失去焦点 3 ...
- HTML5 <video> - 使用 DOM 进行控制
HTML5 <video> 元素同样拥有方法.属性和事件. 其中的方法用于播放.暂停以及加载等.其中的属性(比如时长.音量等)可以被读取或设置.其中的 DOM 事件能够通知您,比方说,&l ...
- 为什么匿名内部类参数必须为final类型
1) 从程序设计语言的理论上:局部内部类(即:定义在方法中的内部类),由于本身就是在方法内部(可出现在形式参数定义处或者方法体处),因而访问方法中的局部变量(形式参数或局部变量)是天经地义的.是很自 ...
- 互联网中一些常用指标(PV、UV、蹦失率、转换率、退出率)
1) PV:PageView 页面点击量,每次刷新就算一次浏览,多次打开同一页面会累加. 通常是衡量网站的主要指标. 2)UV:Unique Visitor一天内访问网站的人数(是以cookie为 ...
- word常用操作
[Word2003文档添加个行号] 参考:http://jingyan.baidu.com/article/e9fb46e1ca1d3c7520f7666f.html#333225-tsina-1-5 ...
- 十DHT11
本实验是按照流程图的顺序去写的文章 据说DHT11没有小数部分,规格书是错误的.不过我验证的程序确实是没有小数 一.硬件连接:DHT11的数据线接到了PB10 二.实验代码 1.开始 2.初始化——串 ...
- iOS: How To Make AutoLayout Work On A ScrollView
iOS: How To Make AutoLayout Work On A ScrollView Posted on June 11th, 2014 Ok, I’ll admit. I’ve been ...
- [转]Linux 的多线程编程的高效开发经验
Linux 平台上的多线程程序开发相对应其他平台(比如 Windows)的多线程 API 有一些细微和隐晦的差别.不注意这些 Linux 上的一些开发陷阱,常常会导致程序问题不穷,死锁不断.本文中我们 ...
- mongodb C# 驱动查询
INoSqlProvider provider = NoSqlManager.Create("CloudTable"); IMongoCollection<FormMongo ...
- Magento显示多货币,Magento 多货币设置
System - Configuration - Currency Setup 在右边Currency Options里的Allowed currencies勾选, 然后 System - Manag ...