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, a solution set is:

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

题意:给定数字n,生成所有可能的n对括号的组合

思路:dfs暴力枚举

当左括号出现的次数 < n 的时候,可以选择放置新的左括号

当右括号出现的次数 < 左括号的次数的时候,可以选择放置新的右括号

递归函数:

void generateparenthesis(int n, int left, int right, vector<string>& result)

表示将生成的所有可能的n对括号的组合放到result中,

其中,

s表示已生成的部分括号,

left表示当前的左括号数,

right表示当前的右括号数

复杂度:时间O(n),空间O(log n)

class Solution {
public:
    void generateParenthesis(int n, int left, int right, string s,vector<string>& result)
    {
    	if(left == n && right == n){
    		result.push_back(s);
    		return ;
    	}
    	if(left < n) generateParenthesis(n, left + 1, right ,s + "(", result);
    	if(right < left) generateParenthesis(n, left, right + 1, s + ")", result);
    }

    vector<string> generateParenthesis(int n){
    	vector<string> result;
    	generateParenthesis(n, 0, 0, "", result);
    	return result;
    }
};

Leetcode 暴力 Generate Parentheses

时间: 2024-11-05 17:33:11

Leetcode 暴力 Generate Parentheses的相关文章

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

[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】Generate Parentheses (2 solutions)

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 022 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】Generate Parentheses in 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: "((()))", "(()())", "(())()", "()(())", "()()()" 用dfs思想做

LeetCode 21 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: "((()))", "(()())", "(())()", "()(())", "()()()" 思路:假设当前

[C++]LeetCode: 84 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: "((()))", "(()())", "(())()", "()(())", "()()()" 思路: