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. IOS之pageControl

    用户点击页面控件,会触发UIControlEventValueChanged事件,并启动设置为控件动作的任何方法.可以通过调用currentPage查询控件的新值,并通过调整numberOfPages ...

  2. ssm框架搭建(上)

    前言 之前也说过,工作做的开发都是基于公司现有的框架,心里很没底.所以一直想自己能够搭建出ssm框架.经过多次尝试,终于成功了.这边文章将从两个方面进行,一是框架搭建,二是简单的增删查改. 正文 1. ...

  3. 【Conclusion】MySQL的安装和使用

    MySQL使用 因为数据库实验用到了MySQL,这里对现在已经涉及到的MySQL部分操作做一个简单的小结. 1.安装MySQL 上MySQL的官网下载对应自己OS平台的MySQL安装文件,有在线安装和 ...

  4. 升级或者重装Discuz! 版本后 QQ互联英文乱码显示的正确解决方法

    升级Discuz! X3版本QQ互联英文乱码!connect_viewthread_share_to_qq!  目前Discuz!论坛上 最简单的解决方法: 第一步:后台----->站长---- ...

  5. 总结vue2.0 配置的实例方法

    总结vue2.0 配置的实例方法 http://www.php.cn/js-tutorial-369603.html

  6. NetBeans 默认编码修改方法

    如果要NetBeans用UTF-8对文件进行解码,需要修改配置文件,具体方法如下: 1. 找到你的Netbeans安装目录下的etc文件夹,如D:\Program Files\NetBeans 8.2 ...

  7. redis.conf介绍

    默认配置文件: # Redis configuration file example. # # Note that in order to read the configuration file, R ...

  8. PHP21 MVC

    学习目标 MVC设计模式 单一入口机制 MVC的实现 MVC设计模式 Model(模型) 是应用程序中用于处理应用程序数据逻辑的部分.通常模型对象负责在数据库中存取数据. View(视图) 是应用程序 ...

  9. No-2.常用 Linux 命令的基本使用

    常用 Linux 命令的基本使用 01. 学习 Linux 终端命令的原因 Linux 刚面世时并没有图形界面,所有的操作全靠命令完成,如 磁盘操作.文件存取.目录操作.进程管理.文件权限 设定等 在 ...

  10. JS数组专题1️⃣ ➖ 数组扁平化

    一.什么是数组扁平化 扁平化,顾名思义就是减少复杂性装饰,使其事物本身更简洁.简单,突出主题. 数组扁平化,对着上面意思套也知道了,就是将一个复杂的嵌套多层的数组,一层一层的转化为层级较少或者只有一层 ...