22. Generate Parentheses
配对构建问题可以考虑使用递归。

 public List<String> generateParenthesis(int n) {
        List<
String> list = new ArrayList<String>();
        backtrack(list,
"", 0, 0, n);
       
return list;
    }
   
   
public void backtrack(List<String> list, String str, int open, int close, int max){
       
       
if(str.length() == max*2){
            list.
add(str);
           
return;
        }
       
       
if(open < max) backtrack(list, str+"(", open+1, close, max);
       
if(close < open)
            backtrack(list,
str+")", open, close+1, max);
    }

相关文章:

  • 2021-11-26
  • 2021-11-26
  • 2021-10-09
  • 2022-01-21
  • 2021-12-29
猜你喜欢
  • 2022-03-06
相关资源
相似解决方案