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.

思路:

本题不像Permutation,不要求罗列所有permutation,所以没必要用回溯递归。

本题如果用Next Permutation,会导致超时。需要寻找规律。

规律:在n!个排列中,除去第一位,后几位共有(n-1)!个排列,所以第一位的元素总是(n-1)!一组出现的。那么,第k行第一位的值就=nums[(k-1)/(n-1)!]。

依次类推第二位的值=nums[(k2-1)/(n-2)!],其中k2=(k-1)%(n-1)! Note: +1是因为k从1开始计数,而非0

class Solution {
public:
string getPermutation(int n, int k) {
string result = "";
bool hasUsed[n+]; //whether the number has used in the string
for(int i = ; i<=n; i++)
{
hasUsed[i] = false;
}
int dp[n]; //permutation number of a digit with i width = i!
dp[] = ;
dp[] = ;
for(int i = ; i< n; i++)
{
dp[i] = i* dp[i-]; //用动态规划法求阶乘
} int num; //记录第i位的数字
stringstream ss;
string str;
for(int i = n; i>; i--)
{
num = k/dp[i-];
if(k%dp[i-] != ) num+=;
int counter = ;
int j;
for(j = ; j<=n && counter!=num; j++)
{
if(!hasUsed[j])counter++; //如果这个数字已经出现,则不计数
if(counter == num) break;
}
hasUsed[j] = true;
ss.clear();
ss<<j;
ss>>str;
result += str;
k = k-(num-)*dp[i-]; //该位的数字订了后,也就意味着已经处理一部分permutation number, 要把这部分去掉
}
return result;
}
};

思路II:

首先,不用把每个状态阶乘都存储,可以求出n! 然后每次遍历除以i便可以得到当前循环所需要的阶乘。

其次,不需要flag,但设一个num[]记录还剩余的数字,这样的好处是,原先要做加法、赋值、比较操作,现在update num只需要赋值操作。

class Solution {
public:
string getPermutation(int n, int k) {
string result = "";
vector<int> num; //num记录第i个数字是多少
int factorial = ;
for(int i = ; i < n; i++){
num.push_back(i+); //给num赋初值
factorial *= num[i]; //求阶乘(n-1)!
} int index; //记录第i位的数字在num[]中的下标
for(int i = n; i > ; i--) //遍历每一位数字
{
factorial /= i; //update factorial
index = (k-)/factorial; //第n位数以(n-1)!为一组,按组出线;index表示第k行(从1开始计数)在第几组(从0开始计数)
k = (k-)%factorial +; //update k
result += (''+num[index]); for(int j = index; j+ < i; j++) //update num
{
num[j] = num[j+];
};
}
return result;
}
};

60. Permutation Sequence (String; Math)的更多相关文章

  1. LeetCode:60. Permutation Sequence,n全排列的第k个子列

    LeetCode:60. Permutation Sequence,n全排列的第k个子列 : 题目: LeetCode:60. Permutation Sequence 描述: The set [1, ...

  2. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  3. [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 ...

  4. 60. Permutation Sequence (JAVA)

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

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

  6. 60. Permutation Sequence

    题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  7. leetcode 60. Permutation Sequence(康托展开)

    描述: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  8. 【LeetCode】60. Permutation Sequence

    题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  9. 【一天一道LeetCode】#60. Permutation Sequence.

    一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

随机推荐

  1. GC之五--SystemGC完全解读

    目录: GC之一--GC 的算法分析.垃圾收集器.内存分配策略介绍 GC之二--GC日志分析(jdk1.8)整理中 GC之三--GC 触发Full GC执行的情况及应对策略 gc之四--Minor G ...

  2. shell 12输入输出重定向

    shell输入/输出重定向 unix系统命令从终端接受输入并将产生的输出发送回终端.命令读取输入的地方,成为标准输入,默认是终端:命令的输出,为标准输出,默认也是终端. command > fi ...

  3. jquery对象和javascript对象的console.log结果

    array.push($("div").children("label")); console.log(array); 输出: 这个是jquery对象,如果在选 ...

  4. 如何在FreePBX ISO 中文版本安装讯时网关,潮流16FXS 网关和潮流话机

    如何在FreePBX ISO 中文版本安装讯时网关,潮流16FXS 网关和潮流话机摘自:http://www.siplab.cn/?p=664 1)迅时的fxo口网关要注册到asterisk,所以现在 ...

  5. Hive基础之Hive的复杂类型

    ARRAY 一组有序字段,字段的类型必须相同.Array(1,2) create table hive_array(ip string, uid array<string>) row fo ...

  6. linux移动复制删除命令

    用mv命令1.作用mv命令来为文件或目录改名或将文件由一个目录移入另一个目录中.该命令等同于DOS系统下的ren和move命令的组合.它的使用权限是所有用户.2.格式mv [options] 源文件或 ...

  7. django-后台sms管理系统的css框架

    地址:https://adminlte.io/ 下载代码.使用index.html的页面及相关文件 通过下在线检查adminlte.io的后台的各种模块元素,仿写.

  8. word自动生成章节标题

    一级目录 二级目录 三级标题

  9. vue基础——vue实例

    创建一个vue实例 每个vue应用都是通过Vue函数创建一个新的Vue实例开始的 var vm = new Vue({ //选项 }) 一个Vue应用由一个通过new Vue创建的根Vue实例,以及可 ...

  10. uiview animation 卡一下

    原因:有个下载图片的地方在主线程执行,导致动画卡一下.