[LeetCode]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, 可以继续放左括号,当右括号数<左括号数时,可以继续放右括号。

 1 class Solution {
 2 public:
 3     vector<string> generateParenthesis(int n) {
 4         vector<string> result;
 5         if (n > 0) {
 6             generateParenthesis(n, "", 0, 0, result);
 7         }
 8
 9         return result;
10     }
11 private:
12     void generateParenthesis(int n, string s, int l, int r, vector<string> &result) {
13         if (l == n) {
14             result.push_back(s.append(n - r, ‘)‘));
15             return;
16         }
17         generateParenthesis(n, s + ‘(‘, l + 1, r, result);
18
19         if (r < l) {
20            generateParenthesis(n, s + ‘)‘, l, r + 1, result);
21         }
22     }
23 };
时间: 2025-01-05 18:13:44

[LeetCode]Generate Parentheses的相关文章

LeetCode: Generate Parentheses [021]

[题目] 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 题解

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 解题报告

Generate ParenthesesGiven n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "

2015.04.01 Leetcode 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 生成括号

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

LeetCode——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: "((()))", "(()())", "(())()", "()(())", "()()()" 原题链接:ht

LeetCode Generate Parentheses 构造括号串(DFS简单题)

题意: 产生n对合法括号的所有组合,用vector<string>返回. 思路: 递归和迭代都可以产生.复杂度都可以为O(2n*合法的括号组合数),即每次产生出的括号序列都保证是合法的. 方法都是差不多的,就是记录当前产生的串中含有左括号的个数cnt,如果出现右括号,就将cnt--.当长度为2*n的串的cnt为0时,就是答案了,如果当前cnt比剩下未填的位数要小,则可以继续装“(”,否则不能再装.如果当前cnt>0,那么就能继续装“)”与其前面的左括号匹配(无需要管匹配到谁,总之能匹配)

【leetcode】Generate Parentheses

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