2019独角兽企业重金招聘Python工程师标准>>>

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):

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

Given n and k, return the kth permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

这种方法超时了。

char A[1000];
class Solution {public:void perm(int i , int n , int k,int & count , string &s , bool tag){if(tag)return;if(i == n){count ++;if(count == k){tag = true;s=A;return;}         }for(int j = i ; j < n ; j ++){swap(A[i],A[j]);perm(i + 1 , n , k , count , s,tag);swap(A[i],A[j]);if(tag)break;}}string getPermutation(int n, int k) {// Note: The Solution object is instantiated only once and is reused by each test case.for(int i = 0 ; i< n ; i ++){A[i] = (char)(i + '1');}string s;int count = 0;bool tag = false;perm(0,n,k,count ,s,tag);return s;}
};

重写之后的算法过了

转载于:https://my.oschina.net/liangxiao/blog/168808

Permutation Sequence相关推荐

  1. 60 Permutation Sequence

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

  2. LeetCode60:Permutation Sequence

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

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

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

  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. 60. Permutation Sequence

    一.题目 1.审题 2.分析 给两个数字 n 与 k,返回 1-n 所有数字组成的从小到大的全排序的第 k 个数. 二.解答 1.思路: 方法一.采用字典序列,返回全部序列后,输出第 k 个.(时间超 ...

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

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

  8. Leetcode Permutation Sequence

    超时算法-排序dfs: class Solution { public: string s; string s1; int count;string getPermutation(int n, int ...

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

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

最新文章

  1. lsm tree java_LSM-tree 基本原理及应用
  2. mysql表格导入java_使用java技术将Excel表格内容导入mysql数据库
  3. printstream java_Java PrintStream
  4. 推荐系统中如何做多目标优化
  5. linux wait函数头文件_手把手教Linux驱动9-等待队列waitq
  6. delphi chart 曲线实时_发展学生曲线跑能力的体育游戏及运用研究
  7. 如何导出源文件_正骨室001期 | 如何避免色彩损失
  8. git clone失败的问题,网络问题
  9. git暂存工作区stash
  10. java怎么让main方法不退出_JAVA线程池原理源码解析—为什么启动一个线程池,提交一个任务后,Main方法不会退出?...
  11. python扫雷脚本_Python自动扫雷实现方法
  12. MFC在指定控件区域内进行一些操作
  13. Java遍历Map集合的四种方式
  14. 机械CAD中如何设置重叠图形消隐?
  15. 国内使用cousera
  16. Excel求和公式的几种用法
  17. 程序员心理小测试:你是否患上抑郁症?
  18. 内部类以及匿名内部类
  19. win10无法连接这个网络的解决方案【亲测有效】
  20. Oracle 19c本地监听配置出现错误-无监听程序

热门文章

  1. javax顶层接口分析
  2. 卡巴斯基:远程工具Ammyy Admin被用来盗取银行账户
  3. 买了云服务器和域名怎么进行解析
  4. 清除NT Kernel System占用80端口
  5. Maven实战(三)Eclipse构建Maven项目
  6. 防止email被抓取的两种方法(js和css)
  7. Windows Live Writer新版新功能试用
  8. (五)磁盘存储空间的管理
  9. rviz学习笔记(一)——Markers: Sending Basic Shapes (C++) 发送基础形状
  10. BZOJ5329: [SDOI2018]战略游戏——题解