【LeetCode】060. 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.
题解:
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的更多相关文章
- 【LeetCode】60. Permutation Sequence 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】60. Permutation Sequence
题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...
- 【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- 【LeetCode】567. Permutation in String 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/permutati ...
- 【leetcode】Next Permutation
Next Permutation Implement next permutation, which rearranges numbers into the lexicographically nex ...
- 【leetcode】Longest Consecutive Sequence(hard)☆
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- 【leetcode】Next Permutation(middle)
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- 【leetcode】Longest Consecutive Sequence
Longest Consecutive Sequence Given an unsorted array of integers, find the length of the longest con ...
- 【LeetCode】Permutations 解题报告
全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...
随机推荐
- 【BZOJ5016】[Snoi2017]一个简单的询问 莫队
[BZOJ5016][Snoi2017]一个简单的询问 Description 给你一个长度为N的序列ai,1≤i≤N和q组询问,每组询问读入l1,r1,l2,r2,需输出 get(l,r,x)表示计 ...
- Scout YYF I (概率+矩阵快速幂)
YYF is a couragous scout. Now he is on a dangerous mission which is to penetrate into the enemy's ba ...
- intellij idea 自动生成setter getter
windows下: alt + insert,然后选择要生成的成员. mac下: command + N
- [luogu3601]签到题
[luogu3601]签到题 luogu 求\[\sum_{i=l}^ri-\phi(i)\] 一个朴素的想法是枚举l~r,根号求\(\phi\),显然这样是\((r-l)\sqrt r\),时间无法 ...
- docker 存储定义成direct-lvm 模式
配置direct-lvm模式 1. 停止Docker systemctl stop docker 2. 安装依赖包 device-mapper-persistent-data,lvm2, and ...
- Java基础教程:多线程基础(1)——基础操作
Java:多线程基础(1) 实现多线程的两种方式 1.继承Thread类 public class myThread extends Thread { /** * 继承Thread类,重写RUN方法. ...
- c++之helloworld与命名空间
首先在linux中需要安装g++编译器. 在中端输入 uname -a,可以查看版本信息. 输入g++,如果提示错误.则需要使用sudo apt-get install g++. #include&l ...
- 多图切换jQuery图片滑块插件
在线演示 本地下载
- Demo Nec
/* 布局 */.g-va{width:1160px;margin:0 auto;} /* visual area */ /* 模块 */.m-nav{position:relative;height ...
- Ansible playbook练习
示例1:创建用户的Playbook --- - name: create user hosts: openstack gather_facts: false tasks: - name: create ...