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

leetcode 22 Generate Parentheses

时间:2019-06-07 11:17:34      阅读:70      评论:0      收藏:0      [点我收藏+]

标签:匹配   tor   back   tco   题目   括号匹配   int   public   leetcode   

题目让你求出所有的 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);
            
    }
};

leetcode 22 Generate Parentheses

标签:匹配   tor   back   tco   题目   括号匹配   int   public   leetcode   

原文地址:https://www.cnblogs.com/Draymonder/p/10987597.html

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