Permutation Sequence(超时,排列问题)
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):
"123"
"132"
"213"
"231"
"312"
"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(超时,排列问题)的更多相关文章
- 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 ...
- [LeetCode]60. Permutation Sequence求全排列第k个
/* n个数有n!个排列,第k个排列,是以第(k-1)/(n-1)!个数开头的集合中第(k-1)%(n-1)!个数 */ public String getPermutation(int n, int ...
- 【LeetCode每天一题】Permutation Sequence(排列序列)
The set [1,2,3,...,n] contains a total of n! unique permutations.By listing and labeling all of the ...
- [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 ...
- 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 ...
- 【一天一道LeetCode】#60. Permutation Sequence.
一天一道LeetCode系列 (一)题目 The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and ...
- LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation]
LeetCode 31 Next Permutation / 60 Permutation Sequence [Permutation] <c++> LeetCode 31 Next Pe ...
- 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 ...
- 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 ...
随机推荐
- pickle 两个使用小方法
def pickle_load(file_path): f = open(file_path,'r+') data = pickle.load(f) f.close() return data ...
- Metinfo 5.3.19管理员密码重置漏洞复现
Metinfo 5.3.19管理员密码重置漏洞 操作系统:Windows 10专业版 kali linux 网站环境:UPUPW 5.3 使用工具:burpsuite 1.7 beta 漏洞分 ...
- 【软件构造】第八章第三节 代码调优的设计模式和I/O
第八章第三节 代码调优的设计模式和I/O 本节学习如何通过对代码的修改,消除性能瓶颈,提高系统性能?——代码调优.面向性 能的设计模式 Outline Java调优 代码调优的概念 单例模式(Sing ...
- pymysql遇到中文编码
明明数据库里的编码方式和字符集都没有问题,用python插入数据时,数据库里的数据还是乱码的 在数据库中插入时,能够正常显示 那就是python导出的数据存在编码问题,代码如下: # coding: ...
- 最小生成树 Prim算法 Kruskal算法实现
最小生成树定义 最小生成树是一副连通加权无向图中一棵权值最小的生成树. 在一给定的无向图 G = (V, E) 中,(u, v) 代表连接顶点 u 与顶点 v 的边(即,而 w(u, v) 代表此边的 ...
- [POJ] 2223 Muddy Fields
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11490 Accepted: 4270 Description Rain has ...
- docker run redis
#拉取 Redis 镜像 C:\Users\WYJ>docker pull redis Using default tag: latest latest: Pulling from librar ...
- redis:安装配置主从
1.安装依赖包 yum install gcc gcc-c++ -y 2.下载安装包,解压 cd /usr/local/src/wget http://download.redis.io/releas ...
- vue 运行时 + 编译器 vs. 只包含运行时
https://cn.vuejs.org/v2/guide/installation.html#运行时-编译器-vs-只包含运行时 文档中的这个地方,说的不清楚 If you need to comp ...
- React深入 - 手写redux api
简介: 手写实现redux基础api createStore( )和store相关方法 api回顾: createStore(reducer, [preloadedState], enhancer) ...