首页 > 其他 > 详细

LeetCode – Refresh – Generate Parentheses

时间:2015-03-20 01:15:42      阅读:265      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!