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 };

 

posted on 2015-03-19 23:18  keepshuatishuati  阅读(148)  评论(0编辑  收藏  举报