Next Permutation&&Permutation Sequence
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
题目的意思是:123的全排列按字典顺序为:
123 132 213 231 312 321
如果输入其中某一个序列,返回它的下一个序列。如:输入:213 输出:231 ;输入:321 输出:123
算法思想:举例如下
输入:1 4 6 5 3 2
step1:从右往左找到第一个破坏升序(非严格)的元素,此例中为4.记下标为 i
step2: 依然从右往左,找到第一个大于4的元素,此例中5,交换4和5.
step3:从i+1到最右端,逆置。6 4 3 2 to 2 3 4 6
so,1 5 2 3 4 6 即为所求。
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int i,j,len=nums.size();
for(i=len-;i>=;--i)
{
if(nums[i+]>nums[i])
{
for(j=len-;j>i-;--j)if(nums[j]>nums[i])break;
swap(nums[i],nums[j]);
reverse(nums.begin()+i+,nums.end());
return;
}
}
reverse(nums.begin(),nums.end());
return;
}
};
Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123""132""213""231""312""321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
解法参考了:http://blog.csdn.net/lanxu_yy/article/details/17261527
思路:
思路1是用NP的方式来罗列出所有的排列再找出第k个结果,这种方法的时间复杂度与空间复杂度比较高。思路2是研究排序结果的规律,例如取n是,结果可以分为n个组,第一组是第一个数字取最小的那个(即1),第k组是取数字排第k小的那个(即k),每组的数字个数是(n-1)!。依次类推可以递归到n为1时。最终k可以表示为k=A1(n-1)!+A2(n-2)!+...+An,其中Ak代表该数为剩余数字中第Ak小的数字。
class Solution {
public:
string getPermutation(int n, int k) {
vector<bool> flag(n,false);
int *A=new int[n];
int base=;
for(int i=;i<n;i++)
base*=i;
int sum=k-;
for(int i=;i<n;i++)
{
A[i]=sum/base;
sum=sum%base;
if(base!=)
base=base/(n--i);
}
string str;
for(int i=;i<n;i++)
for(int j=;j<n;j++)
{
if(!flag[j])
{
if(A[i]==)
{
str.push_back(j+'');
flag[j]=true;
break;
}
else
{
A[i]--;
}
}
}
return str;
}
};
Next Permutation&&Permutation Sequence的更多相关文章
- [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence
一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...
- leetcode总结:permutations, permutations II, next permutation, permutation sequence
Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...
- Permutation Sequence LT60
The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...
- [LeetCode] Find Permutation 找全排列
By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...
- [算法]——全排列(Permutation)以及next_permutation
排列(Arrangement),简单讲是从N个不同元素中取出M个,按照一定顺序排成一列,通常用A(M,N)表示.当M=N时,称为全排列(Permutation).从数学角度讲,全排列的个数A(N,N) ...
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [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 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- 【leetcode】 Permutation Sequence (middle)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
随机推荐
- [LOJ 6000]搭配飞行员
link 其实就是一道二分图匹配板子,我们建立$S$,$T$为源点与汇点,然后分别将$S$连向所有正驾驶员,边权为$1$,然后将副驾驶员与$T$相连,边权为$1$,将数据中给出的$(a,b)$,将$a ...
- MEMCACHE用法详解
MemCache是一个自由.源码开放.高性能.分布式的分布式内存对象缓存系统,用于动态Web应用以减轻数据库的负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高了网站访问的速度. Me ...
- ucenter搭建
使用xftp传到虚拟机.解压[root@ygy130 ~]# unzip -o -d ./Ucenter_1.6 UCenter_1.6.0_SC_UTF8.zip [root@ygy130 ~]# ...
- Linux安装cx_Oracle
安装依赖包 yum -y install gcc python-devel 确认以下变量都已经设置好了 export ORACLE_HOME=/home/oracle/app/oracle/produ ...
- 中国MOOC_面向对象程序设计——Java语言_第3周 对象容器_1查找里程
第3周编程题 查看帮助 返回 第3周编程题.注意程序(包括注释)中不能出现汉字. 依照学术诚信条款,我保证此作业是本人独立完成的. 温馨提示: 1.本次作业属于Online Judge题目,提交后 ...
- Codeforces Round #511 (Div. 2):C. Enlarge GCD(数学)
C. Enlarge GCD 题目链接:https://codeforces.com/contest/1047/problem/C 题意: 给出n个数,然后你可以移除一些数.现在要求你移除最少的数,让 ...
- 工作中常用的Linux命令(不断更新中)
最近工作中用到linux命令,简单总结如下: 1. pwd 查看当前所在的文件路径 2. cd 切换目录 cd .. 切换到上一级目录 3. ls 列出当前文件路径下的所有文件和文件夹 4. ll 是 ...
- A星寻路算法-Mind&Hand(C++)
//注1:Mind & Hand,MIT校训,这里指的理解与实现(动脑也动手) //注2:博文分为两部分:(1)理解部分,为参考其他优秀博文的摘要梳理:(2)代码部分,是C++代码实现的,源码 ...
- POJ 2289 最大流
Jamie's Contact Groups Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 7624 Accepted: ...
- Microsoft office 2013安装图解
Microsoft office 2013安装图解... ================ 简介: Microsoft Office 2013(Office 15)是微软的新一代Office办公软件, ...