LeetCode/solutions/22. Generate Parentheses.md

68 lines
3.2 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [22. Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)
# 思路
给定括号的个数,返回所有可能的合法的括号组成情况。
## 思路一
用str代表一个合法的括号串初始为空。再作如下定义:
* 用left代表当前还能往str加入左括号的个数left初始为n
* 用right代表当前还能往str加入右括号的个数right初始为0即str不能以右括号开头
定义一个递归函数helper:
* 递归出口当left和right都等于0此时str就应该是一个合法的括号串push进结果数组中再返回即可。
* 否则,若`right > 0`, 说明此时可以添加右括号了right应该减一进入递归跳出递归后若`left > 0`则添加左括号left减一right加一。
## 思路二
其实和思路一类似,不过亲测要比思路一快一些。
用str代表一个括号字符串初始为空。再作如下定义:
* left代表使str成为含有n个括号的合法串还应该向str中加入的左括号的个数即若当前str中左括号的个数为k则left=n-kleft初始为n
* right定义同理right初始也为n。注意和思路一不一样
有了以上定义,那么当`left > right`时说明str中左括号数小于右括号数例如"())"则str不合法。
定义一个递归函数helper:
* 递归出口:`left > right`时直接return或者当left和right都等于0此时str就应该是一个合法的括号串push进结果数组中再返回即可。
* 否则,若`right > 0`, 说明此时可以添加右括号了right应该减一进入递归跳出递归后若`left > 0`则添加左括号left减一与思路一不同:right不加一
## 思路一
``` C++
class Solution {
private:
void helper(vector<string> &res, string str, int left, int right){
// left代表此时还能添加的左括号的个数right代表此时能添加上的右括号的个数
if(left == 0 && right == 0){
res.push_back(str);
return;
}
if(right > 0) helper(res, str + ")", left, right - 1);
if(left > 0) helper(res, str + "(", left - 1, right + 1); // 添加一个左括号后就可以多添加一个右括号了
}
public:
vector<string> generateParenthesis(int n) {
vector<string>res;
helper(res, "", n, 0); // 不能一开始就添加右括号所以right=0
return res;
}
};
```
## 思路二
与思路一不同的只有三个地方,下面标了出来
``` C++
class Solution {
private:
void helper(vector<string> &res, string str, int left, int right){
// left代表使str成为含有n个括号的合法串还应该向str中加入的左括号的个数right同理
if(left > right) return; // 不同点1
if(left == 0 && right == 0){
res.push_back(str);
return;
}
if(right > 0) helper(res, str + ")", left, right - 1);
if(left > 0) helper(res, str + "(", left - 1, right); // 不同点2
}
public:
vector<string> generateParenthesis(int n) {
vector<string>res;
helper(res, "", n, n); // 不同点3
return res;
}
};
```