[LeetCode] 022. Generate Parentheses (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql)

Github: https://github.com/illuz/leetcode


022.Generate_Parentheses (Medium)

链接

题目:https://oj.leetcode.com/problems/generate-parentheses/

代码(github):https://github.com/illuz/leetcode

题意

产生有 n 对括号的所有有效字符串。

分析

  1. 用 DFS 可以很快做出来,能加’(‘就加’(‘,能加’)’就加’)’。(下面的 C++ 实现)
  2. 还有很机智方法写出很短的 DFS 。 (Java 实现)
  3. 对于 DFS 都可以进行记忆化,用空间换时间。 (Python 实现)

代码

C++:

class Solution {
private:
	string tmp;
	void dfs(vector<string> &v, int pos, int n, int used) {
		if (pos == n * 2) {
			cout << tmp << endl;
			v.push_back(tmp);
			return;
		}
		if (used < n) {
			tmp.push_back('(');
			dfs(v, pos + 1, n, used + 1);
			tmp.pop_back();
		}
		if (used * 2 > pos) {
			tmp.push_back(')');
			dfs(v, pos + 1, n, used);
			tmp.pop_back();
		}
	}

public:
    vector<string> generateParenthesis(int n) {
		vector<string> res;
		if (n == 0)
			return res;
		tmp = "";
		dfs(res, 0, n, 0);
		return res;
    }
};

Java:

public class Solution {

    public List<String> generateParenthesis(int n) {
        List<String> ret = new ArrayList<String>(), inner, outter;
        if (n == 0) {
            ret.add("");
            return ret;
        }
        if (n == 1) {
            ret.add("()");
            return ret;
        }
        for (int i = 0; i < n; ++i) {
            inner = generateParenthesis(i);
            outter = generateParenthesis(n - i - 1);
            for (int j = 0; j < inner.size(); ++j) {
                for (int k = 0; k < outter.size(); ++k) {
                    ret.add("(" + inner.get(j) + ")" + outter.get(k));
                }
            }
        }
        return ret;
    }
}

Python:

class Solution:
    # @param an integer
    # @return a list of string
    def generateParenthesis(self, n):
        dp = {0: [""], 1: ["()"]}

        def memorial_dfs(n):
            if n not in dp:
                dp[n] = []
                for i in range(n):
                    for inner in memorial_dfs(i):
                        for outter in memorial_dfs(n - i - 1):
                            dp[n].append('(' + inner + ')' + outter)
            return dp[n]

        return memorial_dfs(n)
时间: 2024-10-29 19:11:07

[LeetCode] 022. Generate Parentheses (Medium) (C++/Python)的相关文章

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

Java for LeetCode 022 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

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

Leetcode 暴力 Generate Parentheses

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Generate Parentheses Total Accepted: 11470 Total Submissions: 37301 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3

[LeetCode] 22. 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: "((()))", "(()())", "(())()", "()(())", "()()()" 给一个数字n,

[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] 031. Next Permutation (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 031. Next Permutation (Medium) 链接: 题目:https://oj.leetcode.com/problems/next-permutation/ 代码(github):https://github.com/illuz/leetcode 题意: 求一个序列的下一个排列. 分析: 可以用

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