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 ...
随机推荐
- Javaweb学习笔记4—Reuest&Response
今天来讲javaweb的第四段学习. Request和Response还是比较重要的 老规矩,首先先用一张思维导图来展现今天的博客内容. ps:我的思维是用的xMind画的,如果你对我的思维导图感兴趣 ...
- Redhat5 安装序列号及版本说明
为了保证安装的组件和订阅相匹配,红帽企业 Linux 5 需要输入一个安装号.它被用来配置安装程序来提供正确的软件包.安装号码包含在你的订阅里. 如果您没有输入安装号码,只有核心服务器或 Deskto ...
- jquery.placeholder.min.js让吃屎的IE浏览器支持placeholder去吧
描述:现在都是HTML5时代了,所有的浏览器都支持placeholder,唯独IE不支持.现在我们有了这款插件,IE下终于可以支持了! 图片展示: 兼容浏览器:IE6+/Firefox/Goog ...
- cocos creator 小记
一个游戏场景有若干个节点组成,这些包括渲染节点,UI节点. 这里弱化了Layer层的概念. 一个游戏由若干个场景组件. 每个节点由若干个组件和若干子节点组成. 例如UI节点中的 按钮节点.子节点有 ...
- js&jquery页面加载完执行
js <script type=”text/javascript”> window.onload=function (){ var userName=”xiaoming”; alert(u ...
- docker-compose nginx
docker-compose nginx example source code docker-compose nginx balancing
- saltstack install on centos7
saltstack offical website reference blog summary install virtualbox yum install VirtualBox-5.2 insta ...
- 找回Settings Sync的gist id和token
方法一:如果你本地有缓存参考:https://www.cnblogs.com/zhang1028/p/9514471.html 方法二:如果你电脑重装系统了 1.找回gist id 登陆你的githu ...
- Openjudge-4115-佐助和鸣人
这一题是一道广搜的题目,首先我们通过读入字符串读入每一行,然后顺带找到鸣人的位置. 然后我们初始化之后,就进行广搜,还是广搜的格式,但是要压入队列的条件我们可以稍微变一变,我们可以直接判断下一个要走的 ...
- 用python的Requests库模拟http请求
一.先了解几个重要的http请求头或响应头信息 Request Headers: Host: 描述请求将被发送的目的地,包括,且仅仅包括域名和端口号. Origin: 说明请求从哪里发起的,包括,且仅 ...