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. 【用jersey构建REST服务】系列文章

    1.用Jersey构建RESTful服务1--HelloWorld http://blog.csdn.NET/kkkloveyou/article/details/21391033 2.用Jersey ...

  2. GC 提前晋升

    如果Survivor 空间不足, 那么从 Eden 存活下来的和原来在 Survivor 空间中不够老的对象占满 Survivor 后, 就会提升到老年代, 可以看到这一轮 Minor GC 后老年代 ...

  3. Eclipse修改workspace目录的几种方式

    Eclipse是一款很强的Java IDE,我们在开始的时候,往往设定了默认的workspace,当用久在之后,我们可能要去更改一下workspace的位置.下面有几种方法可以更改workspace的 ...

  4. PHP mysqli 增强 批量执行sql 语句的实现代码

    本篇文章介绍了,在PHP中 mysqli 增强 批量执行sql 语句的实现代码.需要的朋友参考下. mysqli 增强-批量执行sql 语句 <?php //mysqli 增强-批量执行sql ...

  5. 香侬科技独家对话Facebook人工智能研究院首席科学家Devi Parikh

    Facebook 人工智能研究院(FAIR)首席科学家 Devi Parikh 是 2017 年 IJCAI 计算机和思想奖获得者(IJCAI 两个最重要的奖项之一,被誉为国际人工智能领域的「菲尔兹奖 ...

  6. python之路之迭代器与生成器

    一  迭代器 那么在研究迭代器之前首先应该要知道什么是迭代. 迭代:是一个重复的过程,并且每次重复都是建立基于上一次的结果而来的,所以在迭代的过程其实是在不断变化的. 迭代器:就是迭代取值的工具. 那 ...

  7. mongodb中查询返回指定字段

    mongodb中查询返回指定字段   在写vue项目调用接口获取数据的时候,比如新闻列表页我只需要显示新闻标题和发表时间,点击每条新闻进入详情页的时候才会需要摘要.新闻内容等关于此条新闻的所有字段.  ...

  8. Oracle JOB简例

    JOB declare jobno number; begin dbms_job.submit( jobno, 'insert into tmptable1 values(1,1);', to_dat ...

  9. leetcode134

    class Solution { public: inline int get_next(int idx, int size) { ? : idx+; } int aux(int idx, vecto ...

  10. 机器学习入门-使用GridSearch进行网格参数搜索GridSeach(RandomRegressor(), param_grid, cv=3)

    1.GridSeach(RandomRegressor(), param_grid, cv=3) GridSearch第一个参数是算法本身, 第二个参数是传入的参数组合, cv表示的是交叉验证的次数 ...