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:

"((()))", "(()())", "(())()", "()(())", "()()()"

分析:

递归

代码如下:

class Solution {
private:
    vector<string> ret;
public:
    void solve(int dep, int maxDep, int leftNum, int leftNumTotal, string s)
    {
        //如果(个数超出,return
        if (leftNumTotal * 2 > maxDep)
            return;

        //如果长度足够,return
        if (dep == maxDep){
            ret.push_back(s);
            return;
        }

        for(int i = 0; i < 2; i++)

            //加 (
            if (i == 0)
                solve(dep + 1, maxDep, leftNum + 1, leftNumTotal + 1, s + ‘(‘);
            //加 )
            else if (leftNum > 0)
                solve(dep + 1, maxDep, leftNum - 1, leftNumTotal, s + ‘)‘);
    }

    vector<string> generateParenthesis(int n){
        ret.clear();
        solve(0, 2 * n, 0, 0, "");
        return ret;
    }
};
时间: 2025-01-01 23:34:01

LeetCode 022 Generate Parentheses的相关文章

[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 对括号的所有有

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

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