码迷,mamicode.com
首页 > 其他好文 > 详细

Generate Parentheses

时间:2015-09-10 15:56:46      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:

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:

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

 

 1 void CombinationPar(vector<string>& result, string& sample, int deep,   
 2                            int n, int leftNum, int rightNum)  
 3   {  
 4        if(deep == 2*n)  
 5        {  
 6             result.push_back(sample);  
 7             return;  
 8        }  
 9        if(leftNum<n)  
10        {  
11             sample.push_back(();  
12             CombinationPar(result, sample, deep+1, n, leftNum+1, rightNum);  
13             sample.resize(sample.size()-1);  
14        }  
15        if(rightNum<leftNum)  
16        {   
17             sample.push_back());  
18             CombinationPar(result, sample, deep+1, n, leftNum, rightNum+1);  
19             sample.resize(sample.size()-1);  
20        }  
21   }  
22 
23   vector<string> generateParenthesis(int n) {
24        vector<string> result;  
25        string sample;  
26        if(n!= 0)  
27             CombinationPar(result, sample, 0, n, 0, 0);  
28        return result; 
29     }

 

Generate Parentheses

标签:

原文地址:http://www.cnblogs.com/hexhxy/p/4797949.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!