Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit505ea2b

Browse files
one more backtracking solution
1 parent4f275a5 commit505ea2b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎MEDIUM/src/medium/GenerateParentheses.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,30 @@ public static void main(String...args){
3232
intn =3;
3333
List<String>result =test.generateParenthesis(n);
3434
CommonUtils.print(result);
35+
36+
Solution2sol2 =newSolution2();
37+
List<String>result2 =sol2.generateParenthesis(n);
38+
CommonUtils.print(result2);
39+
}
40+
41+
staticclassSolution2{
42+
publicList<String>generateParenthesis(intn) {
43+
List<String>result =newArrayList();
44+
if(n ==0)returnresult;
45+
helper(result,"",n,n);
46+
returnresult;
47+
}
48+
49+
voidhelper(List<String>result,Stringpar,intleft,intright){
50+
if(left >0){
51+
helper(result,par+"(",left-1,right);
52+
}
53+
if(right >left){
54+
helper(result,par+")",left,right-1);
55+
}
56+
if(right ==0){
57+
result.add(par);
58+
}
59+
}
3560
}
3661
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp