Leetcode Permutation Sequence
超时算法—排序dfs:
class Solution {
public:
string s;
string s1;
int count;string getPermutation(int n, int k) {s="123456789";count=0;s1="";dfs(0,n,k);return s1;}void dfs(int depth,int n,int k){if(depth==n&&count<=k){count++;if(count==k){s1=s.substr(0,n);return;}}if(depth<n){dfs(depth+1,n,k);for(int i=depth+1;i<n;i++){string s2=s;swap(s[depth],s[i]);sort(s.begin()+depth+1,s.begin()+n);dfs(depth+1,n,k);s=s2;}}}
};
超时算法—组合dfs
class Solution {
public:
string s1;
bool used[9];
int count;
char c[9];string getPermutation(int n, int k) {for(int i=0;i<9;i++){used[i]=false;c[i]='0';} count=0;s1="";dfs(0,n,k);return s1;}void dfs(int depth,int n,int k){if(depth==n&&count<k){count++;if(count==k){for(int i=0;i<n;i++)s1+=c[i];return;}}if(depth<n){for(int i=0;i<n;i++){if(!used[i]){used[i]=true;c[depth]='0'+i+1;dfs(depth+1,n,k);used[i]=false;}} }}
};
纯数学算法
class Solution {
public:string getPermutation(int n, int k) {string result;vector<int> num(n);int i, d(1), q;for(i=1;i<=n;++i){num[i-1] = i;d *= i; //d=n!}for(i=n;i>=1;--i){d = d/i;q = (k-1)/d;k = k-q*d;result.push_back('0'+num[q]);num.erase(num.begin()+q);}return result;}
};
转载于:https://www.cnblogs.com/tgkx1054/archive/2013/06/02/3114426.html
Leetcode Permutation Sequence相关推荐
- LeetCode Permutation Sequence(求排列中的第k个排列是什么)
思路:使用阶乘,f(n) = f(n -1) * n,在确定第i个数是,用k/f(n - 1 - i) 代码如下: public class Solution {public String getP ...
- 【数字全排列】LeetCode 60. Permutation Sequence
LeetCode 60. Permutation Sequence Solution0: 偷鸡摸狗的做法 class Solution {public:string getPermutation(in ...
- 60 Permutation Sequence
60 Permutation Sequence 题目 The set [1,2,3,-,n] contains a total of n! unique permutations.By listing ...
- [Swift]LeetCode60. 第k个排列 | Permutation Sequence
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- 康拓编码——Permutation Sequence
描述 2.1.13 Permutation Sequence The set [1,2,3,ĉ ,n] contains a total of n! unique permutations. By l ...
- Permutation Sequence
2019独角兽企业重金招聘Python工程师标准>>> The set [1,2,3,-,n] contains a total of n! unique permutations. ...
- LeetCode60:Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- LeetCode刷题(35)--Permutation Sequence
对于第一个数字,剩余共有(n-1)!中排列,因此第一个数字为k/(n-1)!,之后的数字依次类推 import math class Solution(object):def getPermutati ...
- LeetCode 444. Sequence Reconstruction
Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...
最新文章
- 也许是东半球直接底气的分库分表实践了
- 微服务的接入层设计与动静资源隔离
- cpu超频有什么坏处_电脑超频知多少,让电脑打鸡血,怎么搞定
- nginx配置访问图片路径(windows)
- HelloFresh迁移至新的API网关,实现微服务架构
- 机房收费系统--准备工作
- get_locked_objects_rpt.sql
- Android设置toolbar高度,Android基础知识之 Toolbar 的使用
- MongoDB 教程番外篇之添加用户及设置用户权限 ( Rockmongo登陆设置 )
- 我一口气发现7个Safari 0day,苹果奖了7.5万美元
- Clickhouse分片集群性能测试
- 人工智能十大算法_【读书】人工智能和大数据:新智能的诞生/人工智能科学与技术丛书...
- 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞
- 海康、大华NVR网络硬盘录像机录像无插件全平台访问实现—录像回放时间轴功能实现方法
- VXLAN技术产生背景
- 第十六天(配置BPDU,TCN BPDU)
- numpy找非零元素并计数 numpy.nonzero 和 numpy.count_nonzero
- 6.Paper小结——《A Privacy-Preserving and Verifiable FederatedLearning Scheme》
- 【证明】矩阵特征值之和等于主对角线元素之和
- 大学生活该如何度过?思维导图告诉你
热门文章
- python爬虫教程pdf-《Python爬虫开发与项目实战》pdf完整版
- python语言介绍-Python语言的简介
- python简单代码演示效果-用python画爱心及代码演示
- python 编程一日一练-Python每日一练0013
- python爬虫能干什么-Python爬虫可以做什么?
- python表白-python3实现表白神器
- 对于python来说、一个模块就是一个文件-python常用模块
- python从入门到精通怎么样-《Python编程从入门到精通》
- 学python买什么电脑-学Python买什么笔记本电脑?
- python官方网站进不去-解决CentOS7 卡在开机界面进不去登录界面