Leetcode60. Permutation Sequence第k个排列
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。
按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:
- "123"
- "132"
- "213"
- "231"
- "312"
- "321"
给定 n 和 k,返回第 k 个排列。
说明:
- 给定 n 的范围是 [1, 9]。
- 给定 k 的范围是[1, n!]。
示例 1:
输入: n = 3, k = 3 输出: "213"
示例 2:
输入: n = 4, k = 9 输出: "2314"
较为低效的回溯做法
class Solution {
public:
int N;
int K;
int cntk;
vector<bool> visit;
string res;
string getPermutation(int n, int k)
{
N = n;
K = k;
cntk = 0;
visit = vector<bool>(n + 1, false);
DFS(n, 0);
return res;
}
bool DFS(int cnt, int num)
{
if(cnt == 0)
{
cntk++;
if(cntk == K)
{
res = to_string(num);
return true;
}
else
return false;
}
for(int i = 1; i <= N; i++)
{
if(visit[i] == true)
continue;
visit[i] = true;
num = num * 10 + i;
if(DFS(cnt - 1, num))
{
return true;
}
visit[i] = false;
num = (num - i) / 10;
}
return false;
}
};
Leetcode60. Permutation Sequence第k个排列的更多相关文章
- LeetCode60:Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- LeetCode31 Next Permutation and LeetCode60 Permutation Sequence
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Permutation Sequence(超时,排列问题)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [Swift]LeetCode60. 第k个排列 | Permutation Sequence
The set [1,2,3,...,n] contains a total of n! unique permutations. By listing and labeling all of the ...
- 60. Permutation Sequence(求全排列的第k个排列)
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- LeetCode 60. 第k个排列(Permutation Sequence)
题目描述 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "1 ...
- [LeetCode]60. Permutation Sequence求全排列第k个
/* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...
- 【LeetCode每天一题】Permutation Sequence(排列序列)
The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...
- 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 ...
随机推荐
- vue-router 的重定向-redirect
1. 我们主要在index.js重新设置路由的重新定向redirect参数 index.js import Vue from 'vue' import Router from 'vue-router' ...
- MySql General error:2006
当启用模块时发生Mysql数据库错误,错误信息见附件,实际是“General error: 2006 MySQL server has gone away......”错误. 解决方法:找到my.in ...
- 7.spark运行模式
sparkbin目录下 ./pyspark --help http://spark.apache.org/docs/latest/submitting-applications.h ...
- XYIXY.COM短网址在线生成,快速、稳定、永久有效,免费开放网址缩短API接口。
在PHP中使用API 要在PHP程序中使用API,您必须通过file_get_contents或cURL发送GET请求:两者都是可靠的方法,您可以直接复制下面的代码. <?php /**** S ...
- [JZOJ6299] 2019.08.12【NOIP提高组A】工厂
题目 题目大意 工厂内每个人只会操作一些机器. 他们会以随机的顺序来,每次选任意一台机器来操作. 一台机器只能由一个工人来操作. 可以花费一的代价来使某个工人学会一种机器. 问花费最少的代价,使得在所 ...
- [JZOJ6272] 2019.8.4【NOIP提高组A】整除
题目 题目大意 求方程\((x^m-x)\mod n=0\)在整数范围\([1,n]\)的解的个数. \(n=\sum_{i=1}^{c}p_i\) 给出\(c\)和\(p_i\) 思考历程 作为数论 ...
- 校园商铺-4店铺注册功能模块-8店铺注册之Controller层的改造
不合理的地方: 1. 并不需要将InputStream转换成File类型,直接将InputStream传进入交给CommonsMultipartfile去处理就可以了 如果做这样的转换,每次都需要生成 ...
- thinkphp DEFINE标签
DEFINE标签用于中模板中定义常量,用法如下: 直线电机厂家 <define name="MY_DEFINE_NAME" value="3" /> ...
- jenkins实现不同角色查看不同视图
1.安装插件Role-based Authorization Strategy 2.开启插件 系统管理>>>全局安全配置 3.创建角色和用户 4.登陆查看,只能看到travel开头的 ...
- BZOJ 1398: Vijos1382寻找主人 Necklace(最小表示法)
传送门 解题思路 最小表示法.首先对于判断是不是循环同构的串,直接扫一遍用哈希判即可.然后要输出字典序最小的就要用到最小表示法,首先可以把串复制一遍,这样的话就可以把串变成静态操作.如果对于两个位置\ ...