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

Commit551e93b

Browse files
author
applewjg
committed
GenerateParentheses
Change-Id: I8632d814e4e525599e5cd4647fc7126d48c12052
1 parent3bb743b commit551e93b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎GenerateParentheses.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
Author: King, wangjingui@outlook.com
3+
Date: Dec 20, 2014
4+
Problem: Generate Parentheses
5+
Difficulty: Medium
6+
Source: https://oj.leetcode.com/problems/generate-parentheses/
7+
Notes:
8+
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
9+
For example, given n = 3, a solution set is:
10+
"((()))", "(()())", "(())()", "()(())", "()()()"
11+
12+
Solution: Place n left '(' and n right ')'.
13+
Cannot place ')' if there are no enough matching '('.
14+
*/
15+
16+
publicclassSolution {
17+
publicList<String>generateParenthesis(intn) {
18+
ArrayList<String>res =newArrayList<String>();
19+
generateParenthesisRe(res,n,n,"");
20+
returnres;
21+
}
22+
publicvoidgenerateParenthesisRe(ArrayList<String>res,intleft,intright,Strings) {
23+
if (left ==0 &&right ==0)
24+
res.add(s);
25+
if (left >0)
26+
generateParenthesisRe(res,left -1,right,s +"(");
27+
if (right >left)
28+
generateParenthesisRe(res,left,right -1,s +")");
29+
}
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp