题解 | #括号生成#
括号生成
http://www.nowcoder.com/practice/c9addb265cdf4cdd92c092c655d164ca
/**
*
* @param n int整型
* @return string字符串一维数组
*/
function generateParenthesis( n ) {
// write code here
let result = []
//let temp = n.split('');
dfs(n, n, '', result); // 一开始左右括号的数量都为n
return result;
function dfs (left,right,str,result){
if(left > right)return
if(left === 0 && right === 0 ){
result.push(str)
}
if(left > 0){
dfs(left-1,right,str+'(',result)
}
if(right > 0){
dfs(left,right-1,str+')',result)
}
}
}
module.exports = {
generateParenthesis : generateParenthesis
};