【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)法. ...
随机推荐
- u-boot下载模式LCD显示图片修改方法(基于TQ2440)
1.明确液晶型号,这点非常重要,我手头的液晶是天嵌4.3寸屏,让人很郁闷的是液晶背面竟然写着LCD 3.5,这一点让我在上面浪费了好几个小时: 2.根据液晶型号,修改u-boot1.1.6--> ...
- 我为什么选择采用node.js来做新一代的EasyDarwin RTSP开源流媒体服务器
在去年我们还未开始开发基于node.js的新版本EasyDarwin RTSP开源流媒体服务器的时候,我写了一篇博客<对EasyDarwin开源项目后续发展的思考:站在巨人的肩膀上再跳上另一个更 ...
- Entity Framework 4.1:复杂类型
这篇文章将讨论复杂类型. 默认情况下,EF4.1 将类映射到表,这是约定,但是有时候,我们需要模型比表的粒度更细一些. 地址是一个典型的例子,看一下下面的客户类. )] publicstring St ...
- ajax (异步js+xml)
AJAX 基础 AJAX = 异步js+xml 通过与后台服务器进行少量数据交换,实现前台网页局部更新 XMLHttpRequest对象 是 AJAX 的基础 var xmlhttp; if (win ...
- 【python】-- MySQL简介、安装、操作
MySQL简介.安装.操作 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据.我们也可以将数据存储 ...
- python __name__及__main()__的妙处
#hello.py def sayHello(): str="hello" print(str); if __name__ == "__main__": pri ...
- 从springmvc启动日志学习
javaee标准中,tomcat等web容器启动时走web.xml 先将各种contex-param 放到servletcontxt中变成parameter,然后开始启动容器,容器对外提供了liste ...
- 基于卡方的独立性检验原理及R语言实现
在读到<R语言实战>(第二版)P143页有关卡方独立性检验所记 假设检验 假设检验(Test of Hypothesis)又称为显著性检验(Test of Ststistical Sign ...
- TCP标准模板
伪代码 #创建一个TCP服务器 ss = socket() #创建服务器套接字 ss.bind() #把地址绑定到套接字上 ss.listen() #监听连接 inf_loop: #服务器无线循环 c ...
- 深入浅出聊聊企业级API网关
http://architect.dataguru.cn/article-11431-1.html API Gateway(API GW / API 网关),顾名思义,是出现在系统边界上的一个面向 A ...