首页 > 编程语言 > 详细

算法——求n对()有多少种输出方式?

时间:2019-05-11 16:31:12      阅读:138      评论:0      收藏:0      [点我收藏+]

letcode:22

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

    public List<String> generateParenthesis(int n) {
        List<String> res = new ArrayList<>();
        gen(res, n, n, "");
        return res;
    }

    private void gen(List<String> res, int left, int right, String str) {
        if (left == 0 && right == 0) {
            res.add(str);
            return;
        }
        if (left > 0) {
            gen(res, left - 1, right, str + "(");
        }
        if(right > left){
            gen(res, left, right - 1,str + ")");
        }
    }

 

算法——求n对()有多少种输出方式?

原文:https://www.cnblogs.com/gaoquanquan/p/10848993.html

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