超时算法—排序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相关推荐

  1. LeetCode Permutation Sequence(求排列中的第k个排列是什么)

    思路:使用阶乘,f(n) =  f(n -1) * n,在确定第i个数是,用k/f(n - 1 - i) 代码如下: public class Solution {public String getP ...

  2. 【数字全排列】LeetCode 60. Permutation Sequence

    LeetCode 60. Permutation Sequence Solution0: 偷鸡摸狗的做法 class Solution {public:string getPermutation(in ...

  3. 60 Permutation Sequence

    60 Permutation Sequence 题目 The set [1,2,3,-,n] contains a total of n! unique permutations.By listing ...

  4. [Swift]LeetCode60. 第k个排列 | Permutation Sequence

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  5. 康拓编码——Permutation Sequence

    描述 2.1.13 Permutation Sequence The set [1,2,3,ĉ ,n] contains a total of n! unique permutations. By l ...

  6. Permutation Sequence

    2019独角兽企业重金招聘Python工程师标准>>> The set [1,2,3,-,n] contains a total of n! unique permutations. ...

  7. LeetCode60:Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  8. LeetCode刷题(35)--Permutation Sequence

    对于第一个数字,剩余共有(n-1)!中排列,因此第一个数字为k/(n-1)!,之后的数字依次类推 import math class Solution(object):def getPermutati ...

  9. LeetCode 444. Sequence Reconstruction

    Check whether the original sequence org can be uniquely reconstructed from the sequences in seqs. Th ...

最新文章

  1. 也许是东半球直接底气的分库分表实践了
  2. 微服务的接入层设计与动静资源隔离
  3. cpu超频有什么坏处_电脑超频知多少,让电脑打鸡血,怎么搞定
  4. nginx配置访问图片路径(windows)
  5. HelloFresh迁移至新的API网关,实现微服务架构
  6. 机房收费系统--准备工作
  7. get_locked_objects_rpt.sql
  8. Android设置toolbar高度,Android基础知识之 Toolbar 的使用
  9. MongoDB 教程番外篇之添加用户及设置用户权限 ( Rockmongo登陆设置 )
  10. 我一口气发现7个Safari 0day,苹果奖了7.5万美元
  11. Clickhouse分片集群性能测试
  12. 人工智能十大算法_【读书】人工智能和大数据:新智能的诞生/人工智能科学与技术丛书...
  13. 绿盟安全扫描--检测到目标站点存在javascript框架库漏洞
  14. 海康、大华NVR网络硬盘录像机录像无插件全平台访问实现—录像回放时间轴功能实现方法
  15. VXLAN技术产生背景
  16. 第十六天(配置BPDU,TCN BPDU)
  17. numpy找非零元素并计数 numpy.nonzero 和 numpy.count_nonzero
  18. 6.Paper小结——《A Privacy-Preserving and Verifiable FederatedLearning Scheme》
  19. 【证明】矩阵特征值之和等于主对角线元素之和
  20. 大学生活该如何度过?思维导图告诉你

热门文章

  1. python爬虫教程pdf-《Python爬虫开发与项目实战》pdf完整版
  2. python语言介绍-Python语言的简介
  3. python简单代码演示效果-用python画爱心及代码演示
  4. python 编程一日一练-Python每日一练0013
  5. python爬虫能干什么-Python爬虫可以做什么?
  6. python表白-python3实现表白神器
  7. 对于python来说、一个模块就是一个文件-python常用模块
  8. python从入门到精通怎么样-《Python编程从入门到精通》
  9. 学python买什么电脑-学Python买什么笔记本电脑?
  10. python官方网站进不去-解决CentOS7 卡在开机界面进不去登录界面