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 0485d40

Browse files
add question 648
1 parent 5142f14 commit 0485d40

File tree

3 files changed

+60
-3
lines changed

3 files changed

+60
-3
lines changed

‎README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@
3939

4040
- [q56_合并区间](/src/区间合并/q56_合并区间)
4141

42+
### 字典树(前缀树)
43+
44+
- [q648_单词替换](/src/字典树/q648_单词替换)
45+
4246
### 字符串操作
4347

4448
- [q6_Z字形变换](/src/字符串操作/q6_Z字形变换)

‎README_EN.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
- [Question 138 : Copy List with Random Pointer](/src/链表操作/q138_复制带随机指针的链表)
1919
- [Question 206 : Reverse Linked List](/src/链表操作/q206_反转链表)
2020

21-
### Double Pointer Traversal / Sliding Window
21+
### Two Pointers Traversal / Sliding Window
2222

2323
- [Question 3 : Longest Substring Without Repeating Characters](/src/双指针遍历/q3_无重复字符的最长子串)
2424
- [Question 11 : Container With Most Water](/src/双指针遍历/q11_盛最多水的容器)
@@ -29,7 +29,7 @@
2929
- [Question 121 : Best Time to Buy and Sell Stock](/src/双指针遍历/q121_买卖股票的最佳时机)
3030
- [Question 209 : Minimum Size Subarray Sum](/src/双指针遍历/q209_长度最小的子数组)
3131

32-
### Fast and Slow Pointer Traversal
32+
### Fast and Slow Pointers Traversal
3333

3434
- [Question 141 : Linked List Cycle](/src/快慢指针遍历/q141_环形链表)
3535
- [Question 202 : Happy Number](/src/快慢指针遍历/q202_快乐数)
@@ -39,6 +39,10 @@
3939

4040
- [Question 56 : Merge Intervals](/src/区间合并/q56_合并区间)
4141

42+
### Trie
43+
44+
- [Question 648 : Replace Words](/src/字典树/q648_单词替换)
45+
4246
### String Manipulation
4347

4448
- [Question 6 : ZigZag Conversion](/src/字符串操作/q6_Z字形变换)
@@ -118,7 +122,7 @@
118122
- [Question 144 : Binary Tree Preorder Traversal](/src/树的遍历/q144_二叉树的前序遍历)
119123
- [Question 145 : Binary Tree Postorder Traversal](/src/树的遍历/q145_二叉树的后序遍历)
120124

121-
### Binary Search Trees
125+
### Binary Search Tree
122126

123127
- [Question 98 : Validate Binary Search Tree](/src/二叉搜索树相关/q98_验证二叉搜索树)
124128
- [Question 450 : Delete Node in a BST](/src/二叉搜索树相关/q450_删除二叉搜索树中的节点)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package 字典树.q648_单词替换;
2+
3+
import java.util.List;
4+
5+
/**
6+
* 构建字典树(前缀树)o(n)
7+
*/
8+
class Solution {
9+
public String replaceWords(List<String> roots, String sentence) {
10+
TrieNode trie = new TrieNode();
11+
for (String root : roots) {
12+
TrieNode cur = trie;
13+
for (char letter : root.toCharArray()) {
14+
if (cur.children[letter - 'a'] == null) {
15+
cur.children[letter - 'a'] = new TrieNode();
16+
}
17+
cur = cur.children[letter - 'a'];
18+
}
19+
cur.word = root;
20+
}
21+
22+
StringBuilder ans = new StringBuilder();
23+
24+
for (String word : sentence.split(" ")) {
25+
if (ans.length() > 0) {
26+
ans.append(" ");
27+
}
28+
29+
TrieNode cur = trie;
30+
for (char letter : word.toCharArray()) {
31+
if (cur.children[letter - 'a'] == null || cur.word != null) {
32+
break;
33+
}
34+
cur = cur.children[letter - 'a'];
35+
}
36+
ans.append(cur.word != null ? cur.word : word);
37+
}
38+
return ans.toString();
39+
}
40+
}
41+
42+
class TrieNode {
43+
TrieNode[] children;
44+
String word;
45+
46+
TrieNode() {
47+
children = new TrieNode[26];
48+
}
49+
}

0 commit comments

Comments
(0)

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