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,31,3,2
3,2,11,2,3
1,1,51,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):

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "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小的数字。

例如,n=3,k=5时,我们首先将k换算成4(第一个元素为0而不是1),由于(n-1)!=2,所以第一个元素应该是第4/2=2小的元素。第二个元素的k=4%2=0,且(n-1)!=1,所以第二个元素应该是第0小的元素。第三个元素的k=0%1=0,且(n-1)!=1,所以第三个元素应该是第0小元素。一开始的数字为(1,2,3)第一位是第2(0表示第一位)小元素,故取3,第二位是剩余数字(1,2)中第0小的元素,即为1,第三位是剩余数字(2)中第0小的元素,即2。所以结果为312。
 
我刚开始用的就是思路一的方法,结果测试不通过,运行超时,换成思路二的就好了,注意代码27行的break,不然代码就错了。其实感觉这道题和算法关系不大,完全就是在考数学。
 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的更多相关文章

  1. [LeetCode] “全排列”问题系列(二) - 基于全排列本身的问题,例题: Next Permutation , Permutation Sequence

    一.开篇 既上一篇<交换法生成全排列及其应用> 后,这里讲的是基于全排列 (Permutation)本身的一些问题,包括:求下一个全排列(Next Permutation):求指定位置的全 ...

  2. leetcode总结:permutations, permutations II, next permutation, permutation sequence

    Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...

  3. Permutation Sequence LT60

    The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...

  4. [LeetCode] Find Permutation 找全排列

    By now, you are given a secret signature consisting of character 'D' and 'I'. 'D' represents a decre ...

  5. [算法]——全排列(Permutation)以及next_permutation

    排列(Arrangement),简单讲是从N个不同元素中取出M个,按照一定顺序排成一列,通常用A(M,N)表示.当M=N时,称为全排列(Permutation).从数学角度讲,全排列的个数A(N,N) ...

  6. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  7. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. 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 ...

  9. 【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 ...

随机推荐

  1. mobx动态添加observable

    mobx使用extendObservable来动态添加observable属性. extendObservable(target, properties, decorators?, options?) ...

  2. poj:1850 Code(组合数学?数位dp!)

    题目大意:字符的字典序依次递增才是合法的字符串,将字符串依次标号如:a-1 b-2 ... z-26 ab-27 bc-52. 为什么题解都是组合数学的...我觉得数位dp很好写啊(逃 f[pos][ ...

  3. Numpy模块(数值计算)

    Numpy是高性能科学计算和数据分析的基础包.它是pandas等其他各种工具的基础. NumPy的主要功能: ndarray,一个多维数组结构,高效且节省空间 无需循环对整组数据进行快速运算的数学函数 ...

  4. 【DP】【P1586】四方定理

    传送门 Description Input 第一行为一个整数T代表数据组数,之后T行每行一个数n代表要被分解的数 Output 对于每个n输出一行,为方案个数 Sample Input Sample ...

  5. JNA的用法

    JNA(Java Native Access):建立在JNI之上的Java开源框架,SUN主导开发,用来调用C.C++代码,尤其是底层库文件(windows中叫dll文件,linux下是so[shar ...

  6. 基于DCT系数的实时监控中运动目标检测

    本文的主要内容来自2009 Advanced Video and Signal Based Surveillance会议的一篇论文“Real-Time Moving Object Detection ...

  7. 二叉树中和为某一值得路径 java实现

    本题来自<剑指offer> 路径为从根节点到叶节点一条路径,路径经过的各节点数值之和等于某一给定数值,则打印路径上的节点 因为需要打印满足条件的路径节点信息和各节点之和,需要栈记录经过的节 ...

  8. echarts 使用demo

    <!DOCTYPE html> <head> <meta charset="utf-8"> <title>ECharts</t ...

  9. http学习 - 缓存

    对缓存的理解更加深刻,缓存有一个过期时间,现在用的比较多的是 max-age,以前使用 expirt之类的, 然后就是需要向服务器验证是否是最新的,如果不是最新的则需要更新.

  10. vijos 1153 背包+标记

    描述 新一年度的猫狗大战通过SC(星际争霸)这款经典的游戏来较量,野猫和飞狗这对冤家为此已经准备好久了,为了使战争更有难度和戏剧性,双方约定只能选择Terran(人族)并且只能造机枪兵. 比赛开始了, ...