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
|