leetcode 22 Generate Parentheses

题目让你求出所有的 n对括号匹配的情况

class Solution {
public:
    vector<string> generateParenthesis(int n) {
        if(n <= 0)
            return {};
        vector<string> res;
        helper(res, "", n, 0);
        return res;
    }
    
    void helper(vector<string> &res, string s, int l, int r) {
        if(l == 0 && r ==0) {
            res.push_back(s);
            return ;
        }
        if(l > 0)
            helper(res, s + "(", l-1, r+1);
        if(r > 0)
            helper(res, s + ")", l, r-1);
            
    }
};
posted @ 2019-06-07 09:05  Draymonder  阅读(95)  评论(0编辑  收藏  举报