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 Solution {
public List<String> generateParenthesis(int n) {
List list = new ArrayList<String>();
List list_2 = new ArrayList<String>();
String str = new String();
str = "";
if (n == 0)
return null;
if (n == 1) {
list.add("()");
return list;
}
list_2 = generateParenthesis(n - 1);
Iterator ite = list_2.iterator();
while (ite.hasNext()) {
str = (String) ite.next();
list.add("(" + str + ")");
list.add("()"+str);
list.add(str+"()");
}
list.remove(list.size() - 1);
return list;
}
}
Submission Result: Wrong Answer
Input: 4 Output: ["(((())))","()((()))","((()))()","(()(()))","()()(())","()(())()","((())())","()(())()","(())()()","((()()))","()(()())","(()())()","(()()())","()()()()"] Expected: ["(((())))","((()()))","((())())","((()))()","(()(()))","(()()())","(()())()","(())(())","(())()()","()((()))","()(()())","()(())()","()()(())","()()()()"]
public class Solution {
public List<String> generateParenthesis(int n) {
List list = new ArrayList<String>();
List list_2 = new ArrayList<String>();
String str = new String();
str = "";
if (n == 0)
return null;
if (n == 1) {
list.add("()");
return list;
}
list_2 = generateParenthesis(n - 1);
Iterator ite = list_2.iterator();
while (ite.hasNext()) {
str = (String) ite.next();
list.add("(" + str + ")");
list.add("()"+str);
list.add(str+"()");
}
list.remove(list.size() - 1);
return list;
}
}
Submission Result: Wrong Answer
Input: | 4 |
Output: | ["(((())))","()((()))","((()))()","(()(()))","()()(())","()(())()","((())())","()(())()","(())()()","((()()))","()(()())","(()())()","(()()())","()()()()"] |
Expected: | ["(((())))","((()()))","((())())","((()))()","(()(()))","(()()())","(()())()","(())(())","(())()()","()((()))","()(()())","()(())()","()()(())","()()()()"] |
。。。测试了一下发现和list中排列顺序还有关系。。题目也没说
Java代码;
public class Solution {
public List<String> generateParenthesis(int n) {
List list = new ArrayList<String>();
List list_2 = new ArrayList<String>();
String str = new String();
str = "";
if (n == 0)
return null;
if (n == 1) {
list.add("()");
return list;
}
list_2 = generateParenthesis(n - 1);
Iterator ite = list_2.iterator();
while (ite.hasNext()) {
str = (String) ite.next();
list.add("(" + str + ")");
// list.add("()"+str);
// list.add(str+"()");
}
ite = list_2.iterator();
while (ite.hasNext()) {
str = (String) ite.next();
// list.add("("+str+")");
// list.add("()" + str);
list.add(str + "()");
}
ite = list_2.iterator();
while (ite.hasNext()) {
str = (String) ite.next();
// list.add("("+str+")");
list.add("()" + str);
// list.add(str + "()");
}
list.remove(list.size() - 1);
return list;
}
}
Submission Result: Wrong Answer
Input: 4 Output: ["(((())))","((()()))","((())())","(()()())","(()(()))","((()))()","(()())()","(())()()","()()()()","()(())()","()((()))","()(()())","()(())()","()()()()"] Expected: ["(((())))","((()()))","((())())","((()))()","(()(()))","(()()())","(()())()","(())(())","(())()()","()((()))","()(()())","()(())()","()()(())","()()()()"]
我是服了这个排序的逻辑了
Java代码:
public class Solution {
public ArrayList<String> generateParenthesis(int n) {
ArrayList<String> res = new ArrayList<String>();
if (n <= 0)
return res;
StringBuilder sb = new StringBuilder();
generate(n, n, sb, res);
return res;
}
private void generate(int l, int r, StringBuilder sb, ArrayList<String> res) {
if (r < l)
return;
if (l == 0 && r == 0) {
res.add(sb.toString());
}
if (l > 0) {
sb.append("(");
generate(l - 1, r, sb, res);
sb.deleteCharAt(sb.length() - 1);
}
if (r > 0) {
sb.append(")");
generate(l, r - 1, sb, res);
sb.deleteCharAt(sb.length() - 1);
}
}
}
网上找了一个答案,才发现解题思路和我完全不一样。。。怪不得测试集合里的排序这么奇葩
public class Solution {
public List<String> generateParenthesis(int n) {
List list = new ArrayList<String>();
List list_2 = new ArrayList<String>();
String str = new String();
str = "";
if (n == 0)
return null;
if (n == 1) {
list.add("()");
return list;
}
list_2 = generateParenthesis(n - 1);
Iterator ite = list_2.iterator();
while (ite.hasNext()) {
str = (String) ite.next();
list.add("(" + str + ")");
// list.add("()"+str);
// list.add(str+"()");
}
ite = list_2.iterator();
while (ite.hasNext()) {
str = (String) ite.next();
// list.add("("+str+")");
// list.add("()" + str);
list.add(str + "()");
}
ite = list_2.iterator();
while (ite.hasNext()) {
str = (String) ite.next();
// list.add("("+str+")");
list.add("()" + str);
// list.add(str + "()");
}
list.remove(list.size() - 1);
return list;
}
}
Submission Result: Wrong Answer
Input: 4 Output: ["(((())))","((()()))","((())())","(()()())","(()(()))","((()))()","(()())()","(())()()","()()()()","()(())()","()((()))","()(()())","()(())()","()()()()"] Expected: ["(((())))","((()()))","((())())","((()))()","(()(()))","(()()())","(()())()","(())(())","(())()()","()((()))","()(()())","()(())()","()()(())","()()()()"]
Submission Result: Wrong Answer
Input: | 4 |
Output: | ["(((())))","((()()))","((())())","(()()())","(()(()))","((()))()","(()())()","(())()()","()()()()","()(())()","()((()))","()(()())","()(())()","()()()()"] |
Expected: | ["(((())))","((()()))","((())())","((()))()","(()(()))","(()()())","(()())()","(())(())","(())()()","()((()))","()(()())","()(())()","()()(())","()()()()"] |
我是服了这个排序的逻辑了
Java代码:
public class Solution {
public ArrayList<String> generateParenthesis(int n) {
ArrayList<String> res = new ArrayList<String>();
if (n <= 0)
return res;
StringBuilder sb = new StringBuilder();
generate(n, n, sb, res);
return res;
}
private void generate(int l, int r, StringBuilder sb, ArrayList<String> res) {
if (r < l)
return;
if (l == 0 && r == 0) {
res.add(sb.toString());
}
if (l > 0) {
sb.append("(");
generate(l - 1, r, sb, res);
sb.deleteCharAt(sb.length() - 1);
}
if (r > 0) {
sb.append(")");
generate(l, r - 1, sb, res);
sb.deleteCharAt(sb.length() - 1);
}
}
}
网上找了一个答案,才发现解题思路和我完全不一样。。。怪不得测试集合里的排序这么奇葩