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

LeetCode – Refresh – Generate Parentheses

时间:2015-03-20 01:16:21      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:

Nothing fancy, just use recursive.

 1 class Solution {
 2 public:
 3     void getP(vector<string> &result, string current, int left, int right) {
 4         if (left == 0 && right == 0) {
 5             result.push_back(current);
 6             return;
 7         }
 8         if (left > 0) {
 9             getP(result, current + (, left - 1, right + 1);
10         }
11         if (right > 0) {
12             getP(result, current + ), left, right - 1);
13         }
14     }
15     vector<string> generateParenthesis(int n) {
16         vector<string> result;
17         getP(result, "", n, 0);
18         return result;
19     }
20 };

 

LeetCode – Refresh – Generate Parentheses

标签:

原文地址:http://www.cnblogs.com/shuashuashua/p/4352212.html

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