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.

Submission Result: Time Limit Exceede

Last executed input: 9, 94626

我的超时代码:

class Solution {
private:
vector<vector<int>> res;
int my_n;
int my_k;
bool overFlag;
public:
void swap(vector<int> &a,int i,int j){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
void dfs(int dep,vector<int> temp,vector<int> v)
{
if(dep==my_n){
res.push_back(temp);
if(res.size()==my_k)
overFlag=false;
return;
}
for (int i=dep;i<my_n&&overFlag;++i)
{
swap(v,dep,i);
temp.push_back(v[dep]);
dfs(dep+,temp,v);
temp.pop_back();
swap(v,dep,i);
}
}
string getPermutation(int n, int k) {
overFlag=true; string resStr("");
vector<int> v;
my_n=n;
my_k=k;
for (int i=;i<=n;++i)
{
v.push_back(i);
}
vector<int> temp;
dfs(,temp,v); vector<int> resV=res[k-];
for (int i=;i<resV.size();++i)
{
resStr.push_back(resV[i]+'');
}
return resStr;
}
};

Permutation Sequence(超时,排列问题)的更多相关文章

  1. 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 ...

  2. [LeetCode]60. Permutation Sequence求全排列第k个

    /* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...

  3. 【LeetCode每天一题】Permutation Sequence(排列序列)

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

  4. [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 ...

  5. 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 ...

  6. 【一天一道LeetCode】#60. Permutation Sequence.

    一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...

  7. LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]

    LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...

  8. 60. Permutation Sequence (String; Math)

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

  9. LeetCode解题报告—— Jump Game & Merge Intervals & Permutation Sequence

    1. Jump Game Given an array of non-negative integers, you are initially positioned at the first inde ...

随机推荐

  1. sql server 收缩日志文件

    USE 数据库名称 GO ALTER DATABASE 数据库名称 SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE 数据库名称 SET RECOV ...

  2. Python 风格规范

    分号 不要在行尾加分号, 也不要用分号将两条命令放在同一行.     行长度 每行不超过80 个字符 例外: 如果使用Python 2.4 或更早的版本, 导入模块的行可能多于80 个字符. Pyth ...

  3. how to make a function from using global var to not using it

    let say, we want to find the bottom left node in a tree.one way to do it is using global vars: /** * ...

  4. DedeCMS文章标题长度最全修改方法

    有时候DedeCMS首页或者其他页面不能全部展示文章标题,造成读者阅读体验差.一般来说标题精简.概括性强.有本文关键词就是一个好标题.写软文不比写“作文”,也不是论坛的标题党,软文是用来做排名的,主要 ...

  5. linux 安装nginx 集成emq

    1:下载nginx-1.12.2.tar.gz http://nginx.org/en/download.html 2:解压 tar -zxvf  nginx-1.12.2.tar.gz 3:进行co ...

  6. EBS oracle 批量导入更新MOQ(最小拆分量、采购提前期、最小订购量、最小包装量)

    EXCEL的列:组织id,供应商编号,供应商地点,料号,最小拆分量.采购提前期.最小订购量.最小包装量 --采购导入更新MOQ四个值,若有为空的那列,会保留原来的值,不会去更新那列的值 PROCEDU ...

  7. ffmpeg解析TS流

    介绍:  MPEG的系统层编码为不同的应用场景设计了两种格式:  TS(Transport Stream) 和PS(Program Stream), 它们两者之间不具有层级关系, 在逻辑上,它们两者都 ...

  8. ArrayList 和 LinkedList 区别。

    1. ArrayList和LinkedList都是实现了List接口的容器类,用于存储一系列的对象引用.他们都可以对元素的增删改查进行操作. 2. ArrayList是实现了基于动态数组的数据结构,L ...

  9. LeetCode(21)Merge Two Sorted Lists

    题目 Merge two sorted linked lists and return it as a new list. The new list should be made by splicin ...

  10. Spring核心技术(九)——Spring管理的组件和Classpath扫描

    Spring管理的组件和Classpath的扫描 在前文描述中使用到的Spring中的Bean的定义,都是通过指定的XML来配置的.而前文中描述的注解的解析则是在源代码级别来提供配置元数据的.在那些例 ...