Skip to content

Commit f66916f

Browse files
committed
22. Generate Parentheses
1 parent e7c7d6d commit f66916f

File tree

7 files changed

+124
-1
lines changed

7 files changed

+124
-1
lines changed

src/leetcode/_20_/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public class Main {
77
public static void main(String[] args) {
88
Solution solution = new Solution();
9-
System.out.println(solution.isValid("["));
9+
System.out.println(solution.isValid("((()()))()"));
1010
}
1111
}
1212

src/leetcode/_22_/Main.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode._22_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
System.out.println( solution.generateParenthesis(4));
10+
System.out.println( solution.generateParenthesis(4).size());
11+
}
12+
}
13+

src/leetcode/_22_/Solution.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package leetcode._22_;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
class Solution {
7+
public List<String> generateParenthesis(int n) {
8+
List<String> result = new ArrayList<>();
9+
traceBack(result, "", n, n);
10+
return result;
11+
}
12+
13+
private void traceBack(List<String> result, String curStr, int left, int rigth) {
14+
if (left == 0 && rigth == 0) {
15+
result.add(curStr);
16+
return;
17+
}
18+
if (left > rigth) { // 如果 剩余的 右括号比比左括号 少,说明 当前 str 的右括号多,无法匹配,不再递归
19+
return;
20+
}
21+
if (left >= 0) {
22+
traceBack(result, curStr + '(', left - 1, rigth);
23+
}
24+
if (rigth >= 0) {
25+
traceBack(result, curStr + ')', left, rigth - 1);
26+
}
27+
}
28+
}

src/leetcode/_22_/solution.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
### [22\. Generate Parentheses](https://leetcode.com/problems/generate-parentheses/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given _n_ pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
7+
8+
For example, given _n_ = 3, a solution set is:
9+
10+
```
11+
[
12+
"((()))",
13+
"(()())",
14+
"(())()",
15+
"()(())",
16+
"()()()"
17+
]
18+
```
19+
20+
21+
#### Solution
22+
23+
Language: **Java**
24+
25+
```java
26+
class Solution {
27+
   public List<String> generateParenthesis(int n) {
28+
       List<String> result = new ArrayList<>();
29+
       traceBack(result, "", n, n);
30+
       return result;
31+
  }
32+
33+
   private void traceBack(List<String> result, String curStr, int left, int rigth) {
34+
       if (left == 0 && rigth == 0) {
35+
           result.add(curStr);
36+
           return;
37+
      }
38+
       if (left > rigth) { // 如果 剩余的 右括号比比左括号 少,说明 当前 str 的右括号多,无法匹配,不再递归
39+
           return;
40+
      }
41+
       if (left >= 0) {
42+
           traceBack(result, curStr + '(', left - 1, rigth);
43+
      }
44+
       if (rigth >= 0) {
45+
           traceBack(result, curStr + ')', left, rigth - 1);
46+
      }
47+
  }
48+
}
49+
       if (rigth >= 0) {
50+
```

src/leetcode/_23_/Main.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package leetcode._23_;
2+
3+
import leetcode.common.ListNode;
4+
5+
/**
6+
* Created by zhangbo54 on 2019-03-04.
7+
*/
8+
public class Main {
9+
public static void main(String[] args) {
10+
Solution solution = new Solution();
11+
ListNode node = new ListNode(1).setNext(new ListNode(2).setNext(new ListNode(3)).setNext(new ListNode(4)));
12+
System.out.println(solution.swapPairs(node));
13+
}
14+
}
15+

src/leetcode/_23_/Solution.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package leetcode._23_;
2+
3+
import leetcode.common.ListNode;
4+
5+
/**
6+
* Definition for singly-linked list.
7+
* public class ListNode {
8+
* int val;
9+
* ListNode next;
10+
* ListNode(int x) { val = x; }
11+
* }
12+
*/
13+
class Solution {
14+
public ListNode swapPairs(ListNode head) {
15+
return null;
16+
}
17+
}

src/leetcode/_23_/solution.md

Whitespace-only changes.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy