leetcode22

public class Solution {
    public IList<string> GenerateParenthesis(int n)
        {
            List<string> list = new List<string>();
            backtrack(list, "", 0, 0, n);
            return list;
        }

        private void backtrack(List<String> list, String str, int open, int close, int max)
        {

            if (str.Length == max * 2)
            {
                list.Add(str);
                return;
            }

            if (open < max)
            {
                backtrack(list, str + "(", open + 1, close, max);
            }
            if (close < open)
            {
                backtrack(list, str + ")", open, close + 1, max);
            }
        }
}

https://leetcode.com/problems/generate-parentheses/#/description

时间: 2024-11-03 21:44:43

leetcode22的相关文章

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

Leetcode22: Length of Last Word

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space cha

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

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

dfs &#183; leetcode-22.产生括号组

22.产生括号组 string dfs 题面 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses 给定int n,代表n组括号,编码生成所有有效的括号组合(即符合括号嵌套规则) 样例 given n = 3, a solution set is: [ "((()))", "(()())", "(())()

【leetcode-22】括号生成

给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 例如,给出 n = 3,生成结果为: [ "((()))", "(()())", "(())()", "()(())", "()()()"] 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/generate-parentheses 回溯法 回溯算法基本思想:能进则

leetcode22 括号生成

题目:https://leetcode-cn.com/problems/generate-parentheses/ 给出 n 代表生成括号的对数,请你写出一个函数,使其能够生成所有可能的并且有效的括号组合. 样例输入与输出: n = 3 输出: [ "((()))", "(()())", "(())()", "()(())", "()()()"] 思路: 不难想出直接暴力的做法,每个位置上有"(&

leetcode刷题题目分类

字符串 1.KMP字符串匹配 https://leetcode.com/problems/implement-strstr/description/ 2.句子逆序 leetcode151 3.判断一个树是否是另一个树的子树  leetcode242 括号 1.是否是有效括号 leetcode20 2.生成有效的括号序列 leetcode22 3.寻找字符串中最长有效括号序列长度 leetcode32 4.去除无效括号 leetcode301 5.添加括号的不同种方法 leetcode241 原文

回溯、递归、DFS方法

转自:https://www.cnblogs.com/steven_oyj/archive/2010/05/22/1741376.html1. 1.回溯解法框架 我觉得这个递归方法更好理解,迭代的方法我没太看懂. 1: int a[n]; 2: try(int i) 3: { 4: if(i>n) 5: 输出结果; 6: else 7: { 8: for(j = 下界; j <= 上界; j=j+1) // 枚举i所有可能的路径 9: { 10: if(fun(j)) // 满足限界函数和约束