LeetCode22. 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:

[
  "((()))",
  "(()())",
  "(())()",
  "()(())",
  "()()()"
]

这个题目的思路,我才用的DFS。

加设有三个框子,第一个框子放了n个石头,剩余的框子没石头。

我们的目标是将第一个框子的石头全部转移到第三个框子里面。

操作步骤只有两种:

1,从1号框取出1个石头,放入2号框,产生一个左括号。

2,从2号框取出1个石头,放入3号框,产生一个右括号。

其实就是求得所有转移石头的方式。可以采用迭代求解。

我代码中left变量代表1号框子中的石头,right代码2号框的石头。

public class Solution {
    private void _generateParenthesis(int left, int right, int n, List<String> list, StringBuilder strBuilder){
        if(left >0){
            strBuilder.append('(');
            _generateParenthesis(left-1, right+1, n, list,strBuilder);
            strBuilder.deleteCharAt(strBuilder.length()-1);

        }
        if(right !=0){
            strBuilder.append(")");
            _generateParenthesis(left, right-1, n, list, strBuilder);
            strBuilder.deleteCharAt(strBuilder.length()-1);
        }
        if(left==0 && right == 0){
            list.add(strBuilder.toString());
        }
       
    }
    public List<String> generateParenthesis(int n) {
        int left = n;
        int right = 0;
        List<String> list = new ArrayList<>();
        if(n==0){
            return list;
        }
        StringBuilder strBuilder = new StringBuilder();
        _generateParenthesis(left, right, n, list, strBuilder);
        return list;
    }
}

时间复杂度为有效字符串的个数,空间复杂度为有效字符串个数乘以字符串长度。

在论坛上看到更加优秀的解法,该方法采用动态规划,可以有效的降低时间复杂度点击打开链接

 List<List<String>> lists = new ArrayList<>();
        lists.add(Collections.singletonList(""));
        
        for (int i = 1; i <= n; ++i)
        {
            final List<String> list = new ArrayList<>();
            
            for (int j = 0; j < i; ++j)
            {
                for (final String first : lists.get(j))
                {
                    for (final String second : lists.get(i - 1 - j))
                    {
                        list.add("(" + first + ")" + second);
                    }
                }
            }
            
            lists.add(list);
        }
        
        return lists.get(lists.size() - 1);
    }

分析

My method is DP. First consider how to get the result f(n) from previous result f(0)...f(n-1).
Actually, the result f(n) will be put an extra () pair to f(n-1). Let the "(" always at the first position, to produce a valid result, we can only put ")" in a way that there will be i pairs () inside the extra () and n - 1 - i pairs () outside the extra pair.

Let us consider an example to get clear view:

f(0): ""

f(1): "("f(0)")"

f(2): "("f(0)")"f(1), "("f(1)")"

f(3): "("f(0)")"f(2), "("f(1)")"f(1), "("f(2)")"

So f(n) = "("f(0)")"f(n-1) , "("f(1)")"f(n-2) "("f(2)")"f(n-3) ... "("f(i)")"f(n-1-i) ... "(f(n-1)")"


复杂度算法在分析

通过上面的公式可以知道,有效字符串个数为n。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值