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

Commit d933d22

Browse files
Added python solution to 22.GenerateParenthesis (#121)
1 parent b2d5a03 commit d933d22

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution:
2+
def generateParenthesis(self, n: int) -> List[str]:
3+
stack = []
4+
res = []
5+
6+
def parenthesis_backtrack(open_paren, close_paren):
7+
if open_paren == close_paren == n:
8+
res.append("".join(stack))
9+
return
10+
11+
if open_paren < n:
12+
stack.append("(")
13+
parenthesis_backtrack(open_paren + 1, close_paren)
14+
stack.pop()
15+
16+
if open_paren > close_paren:
17+
stack.append(")")
18+
parenthesis_backtrack(open_paren, close_paren + 1)
19+
stack.pop()
20+
21+
parenthesis_backtrack(0, 0)
22+
return res

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /