前言
大四重拾算法与数据结构,所有内容为自己的阶段小结所以并不是技术性文章,如有兴趣阅读,遇到问题不妨给我留个言,万分感谢!
字典树
复杂度为O(l) l为字符串的长度
比较简单直接付码
package Trie;
import java.util.HashMap;
public class Trie {
private class Node {
public boolean isWord;//判断是不是一个单词
public HashMap<Character, Node> next;
public Node() {
isWord = false;
next = new HashMap<>();
}
public Node(boolean isWord) {
this.isWord = isWord;
next = new HashMap<>();
}
}
private Node root;
private int size;
public Trie() {
root = new Node();
}
public int getSize() {
return size;
}
public void add(String word) {
Node cur = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (!cur.next.containsKey(c)) {
cur.next.put(c, new Node());
}
cur = cur.next.get(c);
}
if (!cur.isWord) {
cur.isWord = true;
size++;
}
}
public boolean contains(String word) {
Node cur = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if(!cur.next.containsKey(c)) {
return false;
}
cur = cur.next.get(c);
}
return cur.isWord;
}
}
下面是LeetCode上的题目
211. Add and Search Word - Data structure design
附上解决代码
package Trie;
import java.util.HashMap;
public class LeetCode211 {
private class Node {
public boolean isWord;
public HashMap<Character, Node> next;
public Node() {
isWord = false;
next = new HashMap<>();
}
}
private Node root;
/** Initialize your data structure here. */
public LeetCode211() {
root = new Node();
}
/** Adds a word into the data structure. */
public void addWord(String word) {
Node cur = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (!cur.next.containsKey(c)) {
cur.next.put(c, new Node());
}
cur = cur.next.get(c);
}
if (!cur.isWord) {
cur.isWord = true;
}
}
/**
* Returns if the word is in the data structure. A word could contain the dot
* character '.' to represent any one letter.
*/
public boolean search(String word) {
return search(root, word, 0);
}
private boolean search(Node node, String word, int index) {
if(index == word.length())
return node.isWord;
char c = word.charAt(index);
//如果不是.的话 正常搜索
if (c != '.') {
if (!node.next.containsKey(c)) {
return false;
}
return search(node.next.get(c),word,index + 1);
} else {
//如果是点的话遍历接下来的孩子 进行匹配
for (char nexChar : node.next.keySet()) {
if(search(node.next.get(nexChar),word,index + 1))
return true;
}
return false;
}
}
}
package Trie;
import java.util.HashMap;
/**
* 每一个单词存在一个值 要求给出单词的前缀 计算含有此前缀的单词值的和
* 用递归的方式累加总和 结点的isWord替换成val 添加正常添加
*/
public class LeetCode677 {
private class Node {
public int val;
public HashMap<Character, Node> next;
public Node() {
this.val = 0;
next = new HashMap<>();
}
}
private Node root;
/** Initialize your data structure here. */
public LeetCode677() {
root = new Node();
}
public void insert(String key, int val) {
Node cur = root;
for (int i = 0; i < key.length(); i++) {
char c = key.charAt(i);
if (!cur.next.containsKey(c)) {
cur.next.put(c, new Node());
}
cur = cur.next.get(c);
}
cur.val = val;
}
public int sum(String prefix) {
Node cur = root;
for (int i = 0; i < prefix.length(); i++) {
char c = prefix.charAt(i);
if (!cur.next.containsKey(c)) {
return 0;
}
cur = cur.next.get(c);
}
return sum(cur);
}
private int sum(Node node) {
int res = node.val;
for(char c : node.next.keySet()) {
res += sum(node.next.get(c));
}
return res;
}
}