1 Star 0 Fork 0

徐长贺/Leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
_22.java 1.67 KB
一键复制 编辑 原始数据 按行查看 历史
Fisher Coder 提交于 6年前 . add test for 22
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
/**
* 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:
[
"((()))",
"(()())",
"(())()",
"()(())",
"()()()"
]*/
public class _22 {
public static class Solution1 {
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList();
backtrack(result, "", 0, 0, n);
return result;
}
void backtrack(List<String> result, String str, int left, int right, int max) {
if (str.length() == max * 2) {
result.add(str);
return;
}
if (left < max) {
backtrack(result, str + "(", left + 1, right, max);
}
if (right < left) {
backtrack(result, str + ")", left, right + 1, max);
}
}
}
public static class Solution2 {
public List<String> generateParenthesis(int n) {
List<String> result = new ArrayList();
if (n == 0) {
return result;
}
helper(result, "", n, n);
return result;
}
void helper(List<String> result, String par, int left, int right) {
if (left > 0) {
helper(result, par + "(", left - 1, right);
}
if (right > left) {
helper(result, par + ")", left, right - 1);
}
if (right == 0) {
result.add(par);
}
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/isulong/Leetcode.git
git@gitee.com:isulong/Leetcode.git
isulong
Leetcode
Leetcode
master

搜索帮助