Skip to content

Commit c6aec36

Browse files
author
liwentian
committed
fd
1 parent 2da8695 commit c6aec36

File tree

6 files changed

+81
-30
lines changed

6 files changed

+81
-30
lines changed

doc/Summary.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@
1212

1313
# BackTracking
1414
- **10. Regular Expression Matching**
15+
- **17. Letter Combinations of a Phone Number**
16+
- **22. Generate Parentheses**
1517

google/RECORDS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
261. Graph Valid Tree
66
323. Number of Connected Components in an Undirected Graph
77
10. Regular Expression Matching
8+
17. Letter Combinations of a Phone Number
9+
22. Generate Parentheses
810

911
2017-9-2
1012
329. Longest Increasing Path in a Matrix
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.leetcode.google;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* Created by liwentian on 2017/9/3.
8+
*/
9+
10+
public class GenerateParentheses {
11+
12+
public List<String> generateParenthesis(int n) {
13+
List<String> result = new LinkedList<String>();
14+
dfs(result, n, "", 0, 0);
15+
return result;
16+
}
17+
18+
private void dfs(List<String> result, int n, String str, int left, int right) {
19+
if (left == n && right == n) {
20+
result.add(str);
21+
return;
22+
}
23+
if (left > n || right > n || left < right) {
24+
return;
25+
}
26+
dfs(result, n, str + "(", left + 1, right);
27+
dfs(result, n, str + ")", left, right + 1);
28+
}
29+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.leetcode.google;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* Created by liwentian on 2017/9/3.
8+
*/
9+
10+
public class LetterCombinationsOfAPhoneNumber {
11+
12+
final String[] NUMS = {
13+
"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"
14+
};
15+
16+
public List<String> letterCombinations(String digits) {
17+
List<String> result = new LinkedList<>();
18+
if (digits.isEmpty()) {
19+
return result;
20+
}
21+
dfs(digits, result, 0, "");
22+
return result;
23+
}
24+
25+
private void dfs(String digits, List<String> result, int idx, String cur) {
26+
if (idx == digits.length()) {
27+
result.add(cur);
28+
return;
29+
}
30+
31+
int n = digits.charAt(idx) - '0';
32+
for (char c : NUMS[n].toCharArray()) {
33+
dfs(digits, result, idx + 1, cur + c);
34+
}
35+
}
36+
}

solution/src/main/java/com/inuker/solution/GenerateParentheses.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,20 @@ public class GenerateParentheses {
1313
// 耗时4ms
1414
public List<String> generateParenthesis(int n) {
1515
List<String> result = new LinkedList<String>();
16-
generateParenthesis(result, "", n, 0);
16+
dfs(result, n, "", 0, 0);
1717
return result;
1818
}
1919

20-
private void generateParenthesis(List<String> result, String str, int left, int right) {
21-
if (left < 0 || right < 0) {
20+
private void dfs(List<String> result, int n, String str, int left, int right) {
21+
if (left == n && right == n) {
22+
result.add(str);
2223
return;
2324
}
24-
25-
if (left == 0 && right == 0) {
26-
result.add(str);
25+
if (left > n || right > n || left < right) {
2726
return;
2827
}
29-
30-
generateParenthesis(result, str + "(", left - 1, right + 1);
31-
generateParenthesis(result, str + ")", left, right - 1);
28+
dfs(result, n, str + "(", left + 1, right);
29+
dfs(result, n, str + ")", left, right + 1);
3230
}
3331

3432
// 耗时38ms

test/src/main/java/com/example/Main.java

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example;
22

33
import com.leetcode.google.DecodeString;
4+
import com.leetcode.google.GenerateParentheses;
45
import com.leetcode.google.MissingRanges;
56
import com.leetcode.google.QueueReconstructionByHeight;
67
import com.leetcode.google.SentenceScreenFitting;
@@ -23,27 +24,10 @@
2324
public class Main {
2425

2526
public static void main(String[] args) {
26-
HashMap<String, String> map = new HashMap<>();
27-
map.put("one", null);
28-
map.put("two", "japan");
29-
map.put("three", "france");
30-
31-
map.replaceAll(new BiFunction<String, String, String>() {
32-
@Override
33-
public String apply(String s, String s2) {
34-
return null;
35-
}
36-
});
37-
38-
39-
map.forEach(new BiConsumer<String, String>() {
40-
@Override
41-
public void accept(String s, String s2) {
42-
System.out.println(s + ", " + s2);
43-
}
44-
});
45-
46-
27+
List<String> lists = new GenerateParentheses().generateParenthesis(3);
28+
for (String s : lists) {
29+
System.out.println(s);
30+
}
4731

4832
}
4933

0 commit comments

Comments
 (0)