Leetcode 22. Generate Parentheses [medium] [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:
在这里插入图片描述

Consideration

  1. For length n string, it is the combination of ‘(’ plus all length n-1 string and ‘)’ plus all length n-1 string.
  2. Fort the new string, we check if it is valid by using a balance. If char is ‘(’, we plus balance by 1. Otherwise, we subtract by 1 and check if the balance is negative. If it is negative, this is invalid string. After iterating through the new string, we should check if the final balance is 0.

Solution 1: brute force
Time complexity: O(n*(2^(2n))). As there are 2^(2n) combinations for a n length string. For each n length string, we need to iterate it n times to check if it is a valid string.
space complexity: O(n*(2^(2n)))

class Solution {
    public List<String> generateParenthesis(int n) {
        List<String> combinations = new ArrayList();
        generateAll(new char[2 * n], 0, combinations);
        return combinations;
    }

    private void generateAll(char[] s, int pos, List<String> combinations) {
        if(pos == s.length) {
            if(isValid(s)) {
                combinations.add(new String(s));
            } 
        } else {
            s[pos] = '(';
            generateAll(s, pos+1, combinations);
            s[pos] = ')';
            generateAll(s, pos+1, combinations);
        }  
    }
    
    private boolean isValid(char[] s) {
        int balance = 0;
        for(char c : s) {
            if(c=='(')
                balance++;
            else {
                balance--;
                if(balance < 0)
                    return false;
            }
        }
        return balance== 0;
    }
}

Solution 2: backtracking
Consideration

  1. When we add the ‘(’ or ‘)’ to the string, we guarantee it is a valid string. When add ‘(’, the append is valid if the count of open ‘(’ is less than n. When add’)’, the append is valid if the count of close ‘)’ is less than the count of open ‘)’.
    =
    I don’t understand this part!!!
class Solution {
    public List<String> generateParenthesis(int n) {
        List<String> combinations = new ArrayList();
        backtrack(combinations,"", 0, 0, n);
        return combinations;
    }

    private void backtrack(List<String> combinations, String s, int open, int close, int max) {
        if(s.length() == 2*max)
            combinations.add(s);
        else {
            if(open < max)
                backtrack(combinations, s+"(", open+1, close, max);
            if(close < open)
                backtrack(combinations, s+")", open, close+1, max);
        }
    }    
}

Solution 3: Closure Number
在这里插入图片描述

class Solution {
    public List<String> generateParenthesis(int n) {
        List<String> ans = new ArrayList();
        if (n == 0) {
            ans.add("");
        } else {
            for (int c = 0; c < n; ++c)
                for (String left: generateParenthesis(c))
                    for (String right: generateParenthesis(n-1-c))
                        ans.add("(" + left + ")" + right);
        }
        return ans;
    }
    
}

Reference
https://leetcode.com/problems/generate-parentheses/solution/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值