1957

无聊蛋疼的1957写的低端博客
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

[leetcode]Generate Parentheses

Posted on 2013-11-08 12:57  1957  阅读(151)  评论(0编辑  收藏  举报
class Solution {
public:
    int tot;
    vector<string> ans;
    void search(string p , int left , int right){
        if(left > tot || right > tot) return;
        if(left == tot && right == tot){
            ans.push_back(p);
            return;
        }
        if(left < right) return;
        search(p + "(" , left + 1 , right);
        search(p + ")" , left , right + 1);
    }

    vector<string> generateParenthesis(int n) {
        // Note: The Solution object is instantiated only once and is reused by each test case.
        tot = n;
        ans.clear();
        search("(" , 1 , 0);
        return ans;
    }
};