由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 问一个题
相关主题
用了递归以后,怎么计算空间复杂度?问一个题目
抽空简单说一下昨天的Google Phone Interview一个容易记忆的permutation算法
遍历二叉树除了recursion还有啥好办法?Permutation leetcode-
关于排列组合的题目的算法攒rp,发个L家面经
Non-recursive permutation一道amazon题
有重复元素的全排列,递归算法Exposed上一道string permutation的题
对自己DFS能力彻底的绝望了。这两道leetcode题有更好的答案吗?
经典递归题需要搞懂非递归算法吗?Given a string, find all its permutations without any repetition?
相关话题的讨论汇总
话题: int话题: recursive话题: mp话题: popstack话题: pair
进入JobHunting版参与讨论
1 (共1页)
l*******r
发帖数: 511
1
string permutation non-recursive solution
有没有比较简洁的solution?大家都是怎么做的 呢
c****p
发帖数: 32
2
我记得的就一个字典法。
http://www.codepp.com/default.aspx?g=posts&t=35

【在 l*******r 的大作中提到】
: string permutation non-recursive solution
: 有没有比较简洁的solution?大家都是怎么做的 呢

g*******y
发帖数: 1930
3
#define MP(i,j) make_pair(i,j)
string s;
int n = s.size();
stack> stk;
void PopStack(){
stk.pop();
if(!stk.empty()){
swap(s[stk.top().first] , s[stk.top().second]);
stk.top().second++;
}
}
void permute(){
stk.push(MP(0,0));
pair x;
while(!stk.empty()){
if(stk.size()==n){
cout< PopStack();
}else{
int
g*******y
发帖数: 1930
4
The best way is to convert the recursive version to an equivalent non-
recursive version using explicit stack to simulate function call stack
you may see the original code for recursive version as a reference to understand my code

【在 l*******r 的大作中提到】
: string permutation non-recursive solution
: 有没有比较简洁的solution?大家都是怎么做的 呢

g*******y
发帖数: 1930
5
顺便给大家给练手的题,
用类似的方法,把Binary Tree的三种递归遍历程序改为等价的非递归stack版本
然后把 N叉树的 DFS遍历的递归程序改为等价非递归用stack版本

understand my code

【在 g*******y 的大作中提到】
: The best way is to convert the recursive version to an equivalent non-
: recursive version using explicit stack to simulate function call stack
: you may see the original code for recursive version as a reference to understand my code

1 (共1页)
进入JobHunting版参与讨论
相关主题
Given a string, find all its permutations without any repetition?Non-recursive permutation
输出字串permutation的time complexity是啥?有重复元素的全排列,递归算法
生成一个有重复数的全排列,怎么做比较好对自己DFS能力彻底的绝望了。
请教个题经典递归题需要搞懂非递归算法吗?
用了递归以后,怎么计算空间复杂度?问一个题目
抽空简单说一下昨天的Google Phone Interview一个容易记忆的permutation算法
遍历二叉树除了recursion还有啥好办法?Permutation leetcode-
关于排列组合的题目的算法攒rp,发个L家面经
相关话题的讨论汇总
话题: int话题: recursive话题: mp话题: popstack话题: pair