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,表示有n对括号,输出n对括号所有正确匹配的情况。

用递归来实现,left和right表示左右括号各剩下多少个,left >= right,即左括号剩下的多,则不能插入右括号。

public class Solution {
    private List<string> result = new List<string>();

    public IList<string> GenerateParenthesis(int n)
    {
        string current = "";
        generate(n, n, current);
        return result;
    }

    public void generate(int left, int right, string current)//left 和 right 表示还剩下的左右括号的个数
    {
        if(left == 0 && right == 0)
        {
            result.Add(current);
            return;
        }
        if(left > 0)
        {
            generate(left - 1, right, current + "(");
        }
        if(right > 0 && left < right)
        {
            generate(left, right - 1, current + ")");
        }
    }
}
时间: 2024-10-14 04:43:43

22_Generate Parentheses的相关文章

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: "((()))", "(()())", "(())()", "()(())", "()()()" 这个问题解的个

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 S

#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: "((()))", "(()())", "(())()", "()(())", "()()()" 本题是括号匹配

【leetcode】Generate Parentheses

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

LeetCode 241. Different Ways to Add Parentheses

241. Different Ways to Add Parentheses Add to List Description Submission Solutions Total Accepted: 38849 Total Submissions: 92740 Difficulty: Medium Contributors: Admin Given a string of numbers and operators, return all possible results from comput

leetcode 20 Valid Parentheses

class Solution { public: bool isValid(string s) { stack<char> parentheses; for (int i = 0; i < s.size(); ++i) { if (s[i] == '(' || s[i] == '[' || s[i] == '{') parentheses.push(s[i]); else { if (parentheses.empty()) return false; if (s[i] == ')' &

22. Generate Parentheses——本质:树,DFS求解可能的path

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

32. Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

241. Different Ways to Add Parentheses

241. Different Ways to Add Parentheses https://leetcode.com/problems/different-ways-to-add-parentheses/ 思路就是:首先找到以运算符为根节点,分别计算左子串和右子串的所有结果的集合,然后依次进行组合计算.参考博客http://www.cnblogs.com/ganganloveu/p/4681439.html. 自己的思路错了,直接用两边只用了一个整数去接收左右子串的计算值!! #include