Created
October 26, 2013 07:13
-
-
Save akira-cn/7166271 to your computer and use it in GitHub Desktop.
1004题解法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function convert(from, to){ | |
if(from.split('').sort().join('') != to.split('').sort().join('')){ | |
return []; | |
} | |
var res = []; | |
function proceed(stack, pos, prefix, ops){ | |
//找到一个解 | |
if(prefix === to){ | |
res.push(ops); | |
return; | |
} | |
//不能pop | |
if(to.indexOf(prefix) != 0){ | |
return; | |
} | |
if(pos < from.length){ | |
//先尝试push进堆栈 | |
proceed(stack+from.charAt(pos), pos+1, prefix, ops+'i'); | |
} | |
if(stack.length){ | |
//再尝试pop出来 | |
proceed(stack.slice(0,-1), pos, prefix+stack.slice(-1), ops+'o'); | |
} | |
} | |
proceed('', 0, '', ''); | |
return res; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment