Title link: LeeCode22 parenthesis generation
Title description: I
got the title stunned and thought of a method similar to dp for a long time. The solution of the three brackets is the solution of one bracket and the solution of two brackets. Five The solution of two brackets is the un-splicing of one bracket and four brackets, and the un-splicing of two brackets and three brackets. There is only one special case, that is, the un-splicing of three brackets and the result of two splicing. Add a parenthesis to the outer layer of a parenthesis, and so on, first save one parenthesis and two parentheses, and use an array to save the solution of all possible situations
class Solution {
public static List<String> generateParenthesis(int n) {
ArrayList<String>[] lists=new ArrayList[10005];
ArrayList<String> list = lists[0];
ArrayList<String> listt = lists[1];
list=new ArrayList<>();
listt=new ArrayList<>();
listt.add("()()");
listt.add("(())");
lists[1]=listt;
list.add("()");
lists[0]=list;
for (int i = 2; i < n; i++) {
//存每一个个数的结果集
ArrayList<String> ans=new ArrayList();
for (int j = 0; j < (i/2)+1; j++) {
//当前结果需要的子结果集
ArrayList<String> l1 = lists[j];
ArrayList<String> l2 = lists[i-j-1];
//遍历两个字结果集
for (int k = 0; k < l1.size(); k++) {
for (int l = 0; l < l2.size(); l++) {
String s = l1.get(k);
String s1 = l2.get(l);
//将子结果拼接
String s2 =s+s1;
String s3 =s1+s;
String s4 ="";
//当j==0时就是另外一个子结果集是比当前小1的子结果集
if(j==0){
s4="("+s1+")";
}
if(!ans.contains(s2)){
ans.add(s2);
}
if(s4!=""&&!ans.contains(s4)){
ans.add(s4);
}
if(!ans.contains(s3)){
ans.add(s3);
}
}
}
}
lists[i]=ans;
}
return lists[n-1];
}
}