由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - leetcode里, backtracking的time complexity怎么算,比如permutations这题目
相关主题
请教下leetcode Permutations IIleetcode 3sum
请问大牛们leetcode上的Permutations II请教 permute vector of vectors 如何实现,谢谢大家
调试成功的next_permutation代码这两道leetcode题有更好的答案吗?
T家电面面经并且不解为何被秒拒哪里看leetcode上题的难度?
这种backtracking的问题怎么算时间复杂度?比如palindrom patitioning.问一下permutation的time complexity问题
一道amazon题leetcode的count and say
关于排列组合的题目的算法A Question from leetcode, 求标准解法,本人解的太笨袅
Given a string, find all its permutations without any repetition?请教leetcode Permutations II 解法和code
相关话题的讨论汇总
话题: num话题: int话题: vector话题: res话题: tmp
进入JobHunting版参与讨论
1 (共1页)
t**r
发帖数: 3428
1
leetcode里, backtracking的time complexity怎么算,比如permutations这题目
class Solution {
public:

void perm(vector num,int k,int n, vector > &res){
if (k==n){
res.push_back(num);
}else{
for (int i=k;i<=n;i++){
int tmp = num[k];
num[k]=num[i];
num[i]=tmp;

perm(num,k+1,n,res);

tmp = num[k];
num[k]=num[i];
num[i]=tmp;
}
}
}
vector > permute(vector &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector > res;
perm(num,0,(num.size()-1),res);
return res;
}
};
k*******n
发帖数: 16
2
尝试推了一下,不知道对不对
T(n) = n * T(n - 1) + n
= n * (n - 1) * T(n - 2) + n * (n - 1) + n
= n * (n - 1) * ... * (n - k + 1) * T(n - k)
+n * (n - 1) * ... * (n - k + 1)
+ ... + n * (n - 1) + n
When k =n, T(n - k) = T(0) = 1
T(n) = (n!) + (n!) + (n!) / 2 + ... + n = O(n!) = O(n^n)
t*****3
发帖数: 112
3
permutation的复杂度应该是O(n!),算法过程恰好遍历了所有的排列可能。更general
的感觉复杂度就是试探了的可能解的规模。
s******t
发帖数: 229
4
用数学公式? 高中排列组合思想
s**x
发帖数: 7506
5
Where is the backtracking ?
s****i
发帖数: 65
6
perm(num,k+1,n,res);
后面3句,
tmp = num[k];
num[k]=num[i];
num[i]=tmp;
原先i,k上的值换了,现在又换回来了,下一个for循环跟下一个数换

【在 s**x 的大作中提到】
: Where is the backtracking ?
1 (共1页)
进入JobHunting版参与讨论
相关主题
请教leetcode Permutations II 解法和code这种backtracking的问题怎么算时间复杂度?比如palindrom patitioning.
请教 怎样存下这个string一道amazon题
leetcode 的 permutations 一题 oj 怎么不过关于排列组合的题目的算法
问一道programming peals上的题Given a string, find all its permutations without any repetition?
请教下leetcode Permutations IIleetcode 3sum
请问大牛们leetcode上的Permutations II请教 permute vector of vectors 如何实现,谢谢大家
调试成功的next_permutation代码这两道leetcode题有更好的答案吗?
T家电面面经并且不解为何被秒拒哪里看leetcode上题的难度?
相关话题的讨论汇总
话题: num话题: int话题: vector话题: res话题: tmp