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

【leetcode】Generate Parentheses

时间:2015-04-25 11:58:22      阅读:95      评论: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 class Solution {
 2 public:
 3     void unguarded_generate(vector<string> &result,string curr,int m,int n)
 4     {
 5         if (m==0&&n==0)
 6         {
 7             result.push_back(curr);
 8         }
 9         else
10         {
11             if(m!=0)
12             {
13                 unguarded_generate(result,curr+"(",m-1,n);
14             }
15         
16             if(m<n&&n!=0)
17             {
18                 unguarded_generate(result,curr+")",m,n-1);
19             }
20         }
21     }
22  
23     vector<string> generateParenthesis(int n) 
24     {
25         vector<string> ret;
26         if(n>0)
27         {
28             unguarded_generate(ret,string(),n,n);
29         }
30         return ret;
31     }
32 };

 

【leetcode】Generate Parentheses

标签:

原文地址:http://www.cnblogs.com/jawiezhu/p/4455537.html

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