Generate Parentheses 解答

Question

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:

"((()))", "(()())", "(())()", "()(())", "()()()

Solution 1

Two conditions to check:

if remained left ‘(‘ nums < remained right ‘)‘, for next char, we can put ‘(‘ or ‘)‘.

if remained left ‘(‘ nums = remained right ‘)‘, for next char, we can only put ‘(‘.

Draw out the solution tree, and do DFS.

 1 public class Solution {
 2     public List<String> generateParenthesis(int n) {
 3         List<String> result = new ArrayList<String>();
 4         List<Character> list = new ArrayList<Character>();
 5         dfs(n, n, list, result);
 6         return result;
 7     }
 8
 9     private void dfs(int leftRemain, int rightRemain, List<Character> list, List<String> result) {
10         if (leftRemain < 0 || rightRemain < 0 || leftRemain > rightRemain)
11             return;
12
13         if (leftRemain == 0 && rightRemain == 0) {
14             int size = list.size();
15             StringBuilder sb = new StringBuilder(size);
16             for (char tmpChar : list)
17                 sb.append(tmpChar);
18             result.add(sb.toString());
19             return;
20         }
21
22         if (leftRemain == rightRemain) {
23             list.add(‘(‘);
24             dfs(leftRemain - 1, rightRemain, list, result);
25             list.remove(list.size() - 1);
26         } else {
27             list.add(‘)‘);
28             dfs(leftRemain, rightRemain - 1, list, result);
29             list.remove(list.size() - 1);
30             list.add(‘(‘);
31             dfs(leftRemain - 1, rightRemain, list, result);
32             list.remove(list.size() - 1);
33         }
34     }
35 }

Solution 2

A much simpler solution.

 1 public class Solution {
 2     public List<String> generateParenthesis(int n) {
 3         List<String> result = new ArrayList<String>();
 4         List<Character> list = new ArrayList<Character>();
 5         dfs(n, n, "", result);
 6         return result;
 7     }
 8
 9     private void dfs(int leftRemain, int rightRemain, String prefix, List<String> result) {
10         if (leftRemain < 0 || rightRemain < 0 || leftRemain > rightRemain)
11             return;
12         if (leftRemain == 0 && rightRemain == 0) {
13             result.add(new String(prefix));
14             return;
15         }
16         if (leftRemain > 0)
17             dfs(leftRemain - 1, rightRemain, prefix + "(", result);
18         if (rightRemain > 0)
19             dfs(leftRemain, rightRemain - 1, prefix + ")", result);
20     }
21 }
时间: 2024-10-18 04:37:17

Generate Parentheses 解答的相关文章

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

Generate Parentheses java实现

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: "((()))", "(()())", "(())()", "()(())", "()()()" 本题的关键在于

[LeetCode][JavaScript]Generate Parentheses

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: "((()))", "(()())", "(())()", "()(())", "

LeetCode: Generate Parentheses [021]

[题目] 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: "((()))", "(()())", "(())()", "()(())", "()()()" [题

leetCode 22.Generate Parentheses (生成括号) 解题思路和方法

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: "((()))", "(()())", "(())()", "()(())", "

LeetCode: Generate Parentheses 解题报告

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

【LeetCode】Generate Parentheses (2 solutions)

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: "((()))", "(()())", "(())()", "()(())", "

LeetCode 022 Generate Parentheses

题目描述: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: "((()))", "(()())", "(())()", "()(())",

72. Generate Parentheses &amp;&amp; Valid Parentheses

Generate Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]"