【一天一道LeetCode】#22. Generate Parentheses

一天一道LeetCode

(一)题目

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】#20. Valid Parentheses一文中提到怎么判断该组括号有效,另外在【一天一道LeetCode】#17. Letter Combinations of a Phone Number一文中用到回溯法。本题结合这两题的思路来解决。

假设在位置k之后,如果’(‘的数量还有剩余则可以继续添加;那么要添加’)’,则必须满足其剩余的数量大于0且比’(‘剩余的数量大,才可以添加,否则就不是一个有效的括号组。

class Solution {
public:
    vector<string> ret;
    vector<string> generateParenthesis(int n) {
        string str;
        generate(str , n , n);
        return ret;
    }
    void generate(string str , int m , int n)
    {
        if(m==0)//
        {
            while(n--) str+=‘)‘;//如果‘(’没有剩余的了,就直接把剩余的‘)‘加上
            ret.push_back(str);
            return;
        }
        if(m>0)//如果‘(’剩余,可以继续添加‘(‘
        {
            generate(str+‘(‘,m-1,n);
        }
        if(m<n&&n>0)//如果m<n且n>0,可以继续添加‘)‘
        {
            generate(str+‘)‘,m,n-1);
        }
    }
};
时间: 2024-12-22 17:59:10

【一天一道LeetCode】#22. Generate Parentheses的相关文章

[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] 22. Generate Parentheses Java

题目: 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 [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: "((()))", "(()())", "(())()", "()(())", "()()()" 解

LeetCode 22. Generate Parentheses (括号生成)

题目标签:Backtracking 建立一个 HashMap 来记录 括号的 数量,利用DFS, 先用 左括号, 在用 右括号, 当 右括号用完的时候 返回.具体看code. Java Solution: Runtime:  1 ms, faster than 85.94 % Memory Usage: 39.5 MB, less than 20.00 % 完成日期:12/12/2019 关键点:HashMap class Solution { HashMap<Character, Intege

leetcode 22 Generate Parentheses

class Solution { public: vector<string> generateParenthesis(int n) { set<string> t; if (n == 0) t.insert(""); else { vector<string> pre = generateParenthesis(n - 1); for (auto a : pre) { for (int i = 0; i < a.size(); ++i) {

【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

No.22 Generate Parentheses

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

22. Generate Parentheses(js)

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