add question 648

pull/8/head
yuanguangxin 4 years ago
parent 5142f14aa0
commit 0485d40114

@ -39,6 +39,10 @@
- [q56_合并区间](/src/区间合并/q56_合并区间)
### 字典树(前缀树)
- [q648_单词替换](/src/字典树/q648_单词替换)
### 字符串操作
- [q6_Z字形变换](/src/字符串操作/q6_Z字形变换)

@ -18,7 +18,7 @@
- [Question 138 : Copy List with Random Pointer](/src/链表操作/q138_复制带随机指针的链表)
- [Question 206 : Reverse Linked List](/src/链表操作/q206_反转链表)
### Double Pointer Traversal / Sliding Window
### Two Pointers Traversal / Sliding Window
- [Question 3 : Longest Substring Without Repeating Characters](/src/双指针遍历/q3_无重复字符的最长子串)
- [Question 11 : Container With Most Water](/src/双指针遍历/q11_盛最多水的容器)
@ -29,7 +29,7 @@
- [Question 121 : Best Time to Buy and Sell Stock](/src/双指针遍历/q121_买卖股票的最佳时机)
- [Question 209 : Minimum Size Subarray Sum](/src/双指针遍历/q209_长度最小的子数组)
### Fast and Slow Pointer Traversal
### Fast and Slow Pointers Traversal
- [Question 141 : Linked List Cycle](/src/快慢指针遍历/q141_环形链表)
- [Question 202 : Happy Number](/src/快慢指针遍历/q202_快乐数)
@ -39,6 +39,10 @@
- [Question 56 : Merge Intervals](/src/区间合并/q56_合并区间)
### Trie
- [Question 648 : Replace Words](/src/字典树/q648_单词替换)
### String Manipulation
- [Question 6 : ZigZag Conversion](/src/字符串操作/q6_Z字形变换)
@ -118,7 +122,7 @@
- [Question 144 : Binary Tree Preorder Traversal](/src/树的遍历/q144_二叉树的前序遍历)
- [Question 145 : Binary Tree Postorder Traversal](/src/树的遍历/q145_二叉树的后序遍历)
### Binary Search Trees
### Binary Search Tree
- [Question 98 : Validate Binary Search Tree](/src/二叉搜索树相关/q98_验证二叉搜索树)
- [Question 450 : Delete Node in a BST](/src/二叉搜索树相关/q450_删除二叉搜索树中的节点)

@ -0,0 +1,49 @@
package .q648_;
import java.util.List;
/**
* o(n)
*/
class Solution {
public String replaceWords(List<String> roots, String sentence) {
TrieNode trie = new TrieNode();
for (String root : roots) {
TrieNode cur = trie;
for (char letter : root.toCharArray()) {
if (cur.children[letter - 'a'] == null) {
cur.children[letter - 'a'] = new TrieNode();
}
cur = cur.children[letter - 'a'];
}
cur.word = root;
}
StringBuilder ans = new StringBuilder();
for (String word : sentence.split(" ")) {
if (ans.length() > 0) {
ans.append(" ");
}
TrieNode cur = trie;
for (char letter : word.toCharArray()) {
if (cur.children[letter - 'a'] == null || cur.word != null) {
break;
}
cur = cur.children[letter - 'a'];
}
ans.append(cur.word != null ? cur.word : word);
}
return ans.toString();
}
}
class TrieNode {
TrieNode[] children;
String word;
TrieNode() {
children = new TrieNode[26];
}
}
Loading…
Cancel
Save