首页 > 编程语言 > 详细

[LeetCode][JavaScript]Generate Parentheses

时间:2015-05-26 01:34:47      阅读:215      评论:0      收藏:0      [点我收藏+]

Generate Parentheses 

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

"((()))", "(()())", "(())()", "()(())", "()()()"

https://leetcode.com/problems/generate-parentheses/

 

 


 

 

只有两个节点的bfs

 1 /**
 2  * @param {number} n
 3  * @return {string[]}
 4  */
 5 var generateParenthesis = function(n) { 
 6     var res = [];
 7     bfs("", 0, 0);
 8     return res;
 9 
10     function bfs(tmpStr, countL, countR){
11         if(countL === n && countR === n){
12             res.push(tmpStr);
13             return;
14         }
15         if(countL !== n ){
16             bfs(tmpStr + "(", countL + 1, countR);
17         }
18         if(countL > countR){
19             bfs(tmpStr + ")", countL, countR + 1);
20         }
21     }
22 };

 

 

[LeetCode][JavaScript]Generate Parentheses

原文:http://www.cnblogs.com/Liok3187/p/4529490.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!