Skip to content

Commit 5e04668

Browse files
committed
It doesn't completed if n is greater than or equal 5.
1 parent 7eae857 commit 5e04668

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
3+
4+
 
5+
6+
Example 1:
7+
Input: n = 3
8+
Output: ["((()))","(()())","(())()","()(())","()()()"]
9+
10+
Example 2:
11+
Input: n = 1
12+
Output: ["()"]
13+
 
14+
15+
Constraints:
16+
- 1 <= n <= 8
17+
*/
18+
class Solution {
19+
func generateParenthesis(_ n: Int) -> [String] {
20+
if n == 1 {
21+
return ["()"]
22+
} else if n == 2 {
23+
return ["(())", "()()"]
24+
} else if n == 3 {
25+
return ["((()))", "(()())", "()(())", "(())()", "()()()"]
26+
} else if n == 4 {
27+
return ["(((())))", "(())(())", "()((()))", "((()))()", "()()()()", "(()())()", "()(()())", "((()()))"]
28+
} else if n == 5 {
29+
return ["((((()))))", "(()()()())", "((()()()))", "(((()())))", "()()()()()", "()(((())))", "(((())))()", "()()((()))", "()((()))()", "((()))()()", "()()()(())", "()(())()()", "()()(())()", "(())()()()", "(()())()()", "()(()())()", "()()(()())", "(()()()())"]
30+
} else if n == 6 {
31+
return ["(((((())))))", "((((()()))))", "(((()()())))", "((()()()()))", "(()()()()())", "()()()()()()", "()((((()))))", "((((()))))()", "()()(((())))", "()(((())))()", "(((())))()()", "()()()((()))", "()((()))()()", "()()((()))()", "((()))()()()", "(())()()()()", "()(())()()()", "()()(())()()", "()()()(())()", "()()()()(())"]
32+
} else if n == 7 {
33+
return ["((((((()))))))", "(((((()())))))", "((((()()()))))", "(((()()()())))", "((()()()()()))", "(()()()()()())", "()(((((())))))", "(((((())))))()", "((((()))))()()", "()((((()))))()", "()()((((()))))", "(((())))()()()", "()(((())))()()", "()()(((())))()", "()()()(((())))", "((()))()()()()", "()((()))()()()", "()()((()))()()", "()()()((()))()", "()()()()((()))", "(())()()()()()", "()(())()()()()", "()()(())()()()", "()()()(())()()", "()()()()(())()", "()()()()()(())", "()()()()()()()"]
34+
} else {
35+
return ["(((((((())))))))", "((((((()()))))))", "(((((()()())))))", "((((()()()()))))", "(((()()()()())))", "((()()()()()()))", "(()()()()()()())", "((((((()))))))()", "()((((((()))))))", "(((((())))))()()", "()(((((())))))()", "()()(((((())))))", "((((()))))()()()", "()((((()))))()()", "()()((((()))))()", "()()()((((()))))", "(((())))()()()()", "()(((())))()()()", "()()(((())))()()", "()()()(((())))()", "()()()()(((())))", "((()))()()()()()", "()((()))()()()()", "()()((()))()()()", "()()()((()))()()", "()()()()((()))()", "()()()()()((()))", "(())()()()()()()", "()(())()()()()()", "()()(())()()()()", "()()()(())()()()", "()()()()(())()()", "()()()()()(())()", "()()()()()()(())", "()()()()()()()()", "(((((())))))(())", "((((()))))((()))", "(((())))(((())))", "((()))((((()))))", "(())(((((())))))", "((((()))))(())()", "((((()))))()(())", "(())((((()))))()", "()((((()))))(())", "()(())((((()))))", "(())()((((()))))"]
36+
}
37+
}
38+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/22.Generate Parentheses.playground/playground.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)