Generate Parentheses 解答

本文介绍了一种生成合法括号组合的算法,通过两种不同的深度优先搜索(DFS)方法实现。这两种方法都确保了生成的括号序列是有效的,并且提供了完整的Java代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 }

 

转载于:https://www.cnblogs.com/ireneyanglan/p/4888754.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值