题目:

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.

题解:

Solution 1

class Solution {
public:
string getPermutation(int n, int k) {
string s;
for(int i = ; i < n; ++i){
s += (i + ) + '';
}
for(int i = ; i < k - ; ++i){
next_permutation(s);
}
return s;
}
void next_permutation(string &str){
int n = str.size();
for(int i = n - ; i >= ; --i){
if(str[i] >= str[i + ]) continue;
int j = n - ;
for(; j > i; --j) {
if(str[j] > str[i]) break;
}
swap(str[i], str[j]);
reverse(str.begin() + i + , str.end());
return;
}
reverse(str.begin(), str.end());
}
};

Solution 2

class Solution {
public:
string getPermutation(int n, int k) {
string res;
if(n <= || k <= ){
return res;
}
string num = "";
vector<int> f(n, );
for(int i = ; i < n; ++i){
f[i] = f[i - ] * i;
}
--k;
for(int i = n; i > ; --i){
int j = k / f[i - ];
k %= f[i - ];
res.push_back(num[j]);
num.erase(j, );
}
return res;
}
};

康托编码

Solution 3

class Solution {
public:
string getPermutation(int n, int k) {
string s = "", str;
int factorial = ;
for(int i = ; i < n; ++i){
factorial *= i;
}
--k;
for(int i = n; i > ; --i){
int index = k / factorial;
str += s[index];
s.erase(index, );
k %= factorial;
factorial /= i - ? i - : ;
}
return str;
}
};

【LeetCode】060. Permutation Sequence的更多相关文章

  1. 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  2. 【LeetCode】60. Permutation Sequence

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

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

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

  4. 【LeetCode】567. Permutation in String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/permutati ...

  5. 【leetcode】Next Permutation

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

  6. 【leetcode】Longest Consecutive Sequence(hard)☆

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  7. 【leetcode】Next Permutation(middle)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  8. 【leetcode】Longest Consecutive Sequence

    Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...

  9. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

随机推荐

  1. TP的分页加查询

    1.查询显示数据库的内容 控制器里的内容 public function shouye() { $n = M("car"); $arr = $n->select(); $th ...

  2. 【BZOJ4927】第一题 双指针+DP(容斥?)

    [BZOJ4927]第一题 Description 给定n根直的木棍,要从中选出6根木棍,满足:能用这6根木棍拼 出一个正方形.注意木棍不能弯折.问方案数. 正方形:四条边都相等.四个角都是直角的四边 ...

  3. 测试站如何最快获取正式站的最新数据: ln -s

    针对静态数据, 比如图片/js等文件, 测试站如何获取最新的呢? ln -s /alidata/www/mysite/uploads /alidata/www/mysite_test/uploads ...

  4. ABAP文件选择框函数

    因为WS_FILENAME_GET已经被废弃所以使用接口CL_GUI_FRONTEND_SERVICES来实现本地文件的选择. 用接口类CL_GUI_FRONTEND_SERVICES实现的方法 CA ...

  5. Git——基本思想和工作原理(二)

    核心知识点: 1.Git关注文件数据的整体是否发生变化,对更新的文件做一个快照,然后保存一个指向快照的索引,而不会关注文件数据的具体变化. 2.Git版本的更新几乎都发生在本地,不会因为没有网络而不能 ...

  6. pinpoint本地开发——collector

    本地启动collector 启动前准备 启动之前,要先确保本地已经可以正常package,install 必须保证install成功,才能进行后续步骤,无法install或者package参考[pin ...

  7. [原创]关于tomcat启动时时候端口被占用,8080,8005,8009

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  8. python有哪些关键字?让他自己“吐”出来!

    通过调用库来输出!for循环控制! 源代码: import keyword c = 0 for i in keyword.kwlist: print(i) c += 1 代码截图: 哈哈,关键字: F ...

  9. shell文件相关指令

    文件解压缩tar 请参考文档:http://blog.csdn.net/eroswang/article/details/5555415/ tar -zcvf ${standardpath}${fil ...

  10. 也谈设计模式Facade

    门面模式提供一个高层次的接口,使得子系统更容易使用. 子系统与外部系统的通信必须经过一个统一的facade进行处理. 内部系统的交互在facade中进行整合,这样,对于外部系统的使用者就不用关注内部系 ...