首页 > 其他 > 详细

leetcode 22 Generate Parentheses

时间:2019-06-07 10:28:47      阅读:77      评论:0      收藏:0      [点我收藏+]

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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!