LeetCode 22. Generate Parentheses分析代码

时间:2022-06-07
本文章向大家介绍LeetCode 22. Generate Parentheses分析代码,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is:

image.png 给定 n 对括号,请写一个函数以将其生成新的括号组合,并返回所有组合结果。

分析

深度优先搜索,关键是找限制条件,右括号的数量不能比左括号数量多。

代码

public class Solution {
    public List<String> generateParenthesis(int n) {
        List<String> res = new ArrayList<>();
        if(n <= 0)
            return res;
        String parent = "";
        dfs(res, "",n , n);
        return res;
    }
    
    private void dfs(List<String> res, String parent, int left, int right) {
        
        if(left == 0 && right == 0) {
            res.add(parent);
            return;
        }
        
        if(left > 0) {
            dfs(res, parent+"(",left-1,right);
        }
        if(right > 0 && right > left) {
            dfs(res, parent + ")",left,right-1);
        }
    }
}