public class Solution { public IList<string> GenerateParenthesis(int n) { List<string> list = new List<string>(); backtrack(list, "", 0, 0, n); return list; } private 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); } } }
https://leetcode.com/problems/generate-parentheses/#/description
时间: 2024-11-03 21:44:43