[Algorithm -- LeetCode] (022) Brackets generation

insert image description here

1. Topic

The number n represents the logarithm of generating parentheses. Please design a function that can generate all possible and valid combinations of parentheses.

Example 1:

Input : n = 3
Output : ["((()))", "(()())", "(())()", "()(())", "()()()"]

Example 2:

Input : n = 1
Output : ["()"]

Link to the topic: https://leetcode.cn/problems/generate-parentheses

2. Diagram

Step 1 : Define state dp[i]

Combinations that can be generated using the i pair of parentheses.

Note : Each state is in the form of a list.

Step 2: State transition equation:

  • A combination of i pair of brackets, obtained on the basis of i - 1 pair of brackets;
  • i A combination of parentheses must start with a left bracket "(" (not necessarily end with ")"), so we can enumerate the positions of the right bracket ")" to get all the combinations;
  • The way of enumeration is to enumerate the possible legal parenthesis pairs between the left parenthesis "(" and the right parenthesis ")", and the remaining legal parenthesis pairs are behind the right parenthesis ")" paired with the first left parenthesis "(", which uses the previous state.

The state transition equation is:

dp[i] = "(" + dp[可能的括号对数] + ")" + dp[剩下的括号对数]

  • The sum of "possible pairs of brackets" and "remaining numbers of brackets" is i, so "possible numbers of brackets" j can start from 0 and cannot exceed i at most, i.e. i – 1;
  • "Number of pairs of brackets remaining" + j = i – 1, so "Number of pairs of brackets remaining" = i – j – 1.
    Organized:

dp[i] = "(" + dp[j] + ")" + dp[i- j - 1] , j = 0, 1, ..., i - 1

Step 3: Think about the initial state and output :

Initial state: Because we need the state of 0 pairs of brackets, the state array dp starts from 0, and 0 brackets is of course [""].
Output: dp[n].
This method is called dynamic programming for the time being, and it is amazing to use it in this way. It has the following two characteristics:

1. Bottom-up: start with small-scale problems, and gradually get the solution set of large-scale problems;

2. No aftereffect: the obtaining of later results will not affect the previous results.

3. Java sample code

class Solution {
    
    
    public List<String> generateParenthesis(int n) {
    
    
        if (n == 0) {
    
    
            return new ArrayList<>();
        }
        // 这里 dp 数组我们把它变成列表的样子,方便调用而已
        List<List<String>> dp = new ArrayList<>(n);

        List<String> dp0 = new ArrayList<>();
        dp0.add("");
        dp.add(dp0);

        for (int i = 1; i <= n; i++) {
    
    
            List<String> cur = new ArrayList<>();
            for (int j = 0; j < i; j++) {
    
    
                List<String> str1 = dp.get(j);
                List<String> str2 = dp.get(i - 1 - j);
                for (String s1 : str1) {
    
    
                    for (String s2 : str2) {
    
    
                        // 枚举右括号的位置
                        cur.add("(" + s1 + ")" + s2);
                    }
                }
            }
            dp.add(cur);
        }
        return dp.get(n);
    }
}

Results of the:
insert image description here

Guess you like

Origin blog.csdn.net/duoduo_11011/article/details/131795479