Skip to content

Commit 498064b

Browse files
Merge pull request #215 from hiColors/master
第四周作业#9
2 parents 410b4a6 + e7cec4f commit 498064b

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed

Week_04/id_9/LeetCode_211_9.java

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.github.lifelab.leetcode.problemset;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Objects;
6+
import java.util.regex.Pattern;
7+
8+
/**
9+
* 添加与搜索单词 - 数据结构设计 @see https://leetcode-cn.com/problems/add-and-search-word-data-structure-design/
10+
*
11+
* @author Weichao Li ([email protected])
12+
* @since 2019-06-30
13+
*/
14+
public class Solution211 {
15+
16+
/**
17+
* hash + cache 实现
18+
*/
19+
public class WordDictionary1 {
20+
21+
private Map<String, String> wd;
22+
23+
private Map<String, Boolean> cache;
24+
25+
/**
26+
* Initialize your data structure here.
27+
*/
28+
public WordDictionary1() {
29+
this.wd = new HashMap<>();
30+
this.cache = new HashMap<>();
31+
}
32+
33+
/**
34+
* Adds a word into the data structure.
35+
*/
36+
public void addWord(String word) {
37+
wd.put(word, word);
38+
}
39+
40+
/**
41+
* Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
42+
*/
43+
public boolean search(String word) {
44+
Boolean cacheValue = cache.get(word);
45+
if (Objects.nonNull(cacheValue) && cacheValue) {
46+
return cacheValue;
47+
} else {
48+
cacheValue = wd.keySet().parallelStream().anyMatch(e -> Pattern.matches(word, e));
49+
cache.put(word, cacheValue);
50+
}
51+
return cacheValue;
52+
}
53+
54+
}
55+
56+
/**
57+
* tree 实现
58+
*/
59+
public class WordDictionary2 {
60+
61+
class Node {
62+
63+
private Node[] next;
64+
65+
private boolean isWord;
66+
67+
public Node() {
68+
next = new Node[26];
69+
isWord = false;
70+
}
71+
}
72+
73+
private Node root;
74+
75+
/**
76+
* Initialize your data structure here.
77+
*/
78+
public WordDictionary2() {
79+
root = new Node();
80+
}
81+
82+
/**
83+
* Adds a word into the data structure.
84+
*/
85+
public void addWord(String word) {
86+
int len = word.length();
87+
Node curNode = root;
88+
for (int i = 0; i < len; i++) {
89+
char curChar = word.charAt(i);
90+
Node next = curNode.next[curChar - 'a'];
91+
if (next == null) {
92+
curNode.next[curChar - 'a'] = new Node();
93+
}
94+
curNode = curNode.next[curChar - 'a'];
95+
}
96+
if (!curNode.isWord) {
97+
curNode.isWord = true;
98+
}
99+
}
100+
101+
/**
102+
* Returns if the word is in the data structure. A word could contain the dot character '.' to represent any one letter.
103+
*/
104+
public boolean search(String word) {
105+
return match(word, root, 0);
106+
}
107+
108+
private boolean match(String word, Node node, int start) {
109+
if (start == word.length()) {
110+
return node.isWord;
111+
}
112+
char alpha = word.charAt(start);
113+
if (alpha == '.') {
114+
for (int i = 0; i < 26; i++) {
115+
if (node.next[i] != null && match(word, node.next[i], start + 1)) {
116+
return true;
117+
}
118+
}
119+
return false;
120+
} else {
121+
if (node.next[alpha - 'a'] == null) {
122+
return false;
123+
124+
}
125+
return match(word, node.next[alpha - 'a'], start + 1);
126+
}
127+
}
128+
}
129+
130+
}

Week_04/id_9/LeetCode_78_9.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github.lifelab.leetcode.problemset;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
/**
7+
* 子集 @see https://leetcode-cn.com/problems/subsets/
8+
*
9+
* @author Weichao Li ([email protected])
10+
* @since 2019-06-30
11+
*/
12+
public class Solution78 {
13+
14+
public List<List<Integer>> subsets(int[] nums) {
15+
16+
List<List<Integer>> list = new LinkedList<>();
17+
traverse(nums, 0, list, new LinkedList<>());
18+
return list;
19+
20+
}
21+
22+
private void traverse(int[] nums, int index, List<List<Integer>> list, List<Integer> currentList) {
23+
24+
//terminal
25+
if (nums.length == index) {
26+
list.add(currentList);
27+
return;
28+
}
29+
// process & drill down
30+
List<Integer> target = new LinkedList<>(currentList);
31+
32+
target.add(nums[index]);
33+
34+
traverse(nums, index + 1, list, currentList);
35+
36+
traverse(nums, index + 1, list, target);
37+
}
38+
}

0 commit comments

Comments
 (0)