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:

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

题解: 很容易想到DFS, 采用递归的方式进行,比较难把握的是每次递归的结果用什么来存储,终止条件是什么。

需要注意的是,在每次递归的时候剩余左括号不可能比右括号多,否则就终止。

 1 class Solution {
 2 public:
 3     vector<string> vi;
 4     void generateOne(int left,int right,string s)
 5     {
 6        if(left==0)
 7        {
 8            vi.push_back(s+string(right,‘)‘));
 9            return ;
10        }
11        if(left>=right)
12            generateOne(left-1,right,s+‘(‘);
13        else
14        {
15            generateOne(left-1,right,s+‘(‘);
16            generateOne(left,right-1,s+‘)‘);
17        }
18     }
19     vector<string> generateParenthesis(int n) {
20         int left=n,right=n;
21         vi.clear();
22         generateOne(n,n,"");
23         return vi;
24     }
25 };

转载请注明出处: http://www.cnblogs.com/double-win/ 谢谢!

LeetCode: Generate Parentheses 题解

时间: 2024-08-07 17:01:49

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

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

题目描述: 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 构造括号串(DFS简单题)

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

Generate Parentheses leetcode 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: "((()))", "(()())", "(())()", "()(())", "()()()" 题解:

【leetcode】Generate Parentheses

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