auto commit
This commit is contained in:
parent
f91559d3b5
commit
343a5fa6b8
|
@ -5534,6 +5534,11 @@ Trie,又称前缀树或字典树,用于判断字符串是否存在或者是
|
||||||
```java
|
```java
|
||||||
class Trie {
|
class Trie {
|
||||||
|
|
||||||
|
private class Node {
|
||||||
|
Node[] childs = new Node[26];
|
||||||
|
boolean isLeaf;
|
||||||
|
}
|
||||||
|
|
||||||
private Node root = new Node();
|
private Node root = new Node();
|
||||||
|
|
||||||
public Trie() {
|
public Trie() {
|
||||||
|
@ -5543,41 +5548,43 @@ class Trie {
|
||||||
insert(word, root);
|
insert(word, root);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void insert(String word, Node node){
|
private void insert(String word, Node node) {
|
||||||
int idx = word.charAt(0) - 'a';
|
if (node == null) return;
|
||||||
if(node.child[idx] == null){
|
if (word.length() == 0) {
|
||||||
node.child[idx] = new Node();
|
node.isLeaf = true;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if(word.length() == 1) node.child[idx].isLeaf = true;
|
int index = indexForChar(word.charAt(0));
|
||||||
else insert(word.substring(1), node.child[idx]);
|
if (node.childs[index] == null) {
|
||||||
|
node.childs[index] = new Node();
|
||||||
|
}
|
||||||
|
insert(word.substring(1), node.childs[index]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean search(String word) {
|
public boolean search(String word) {
|
||||||
return search(word, root);
|
return search(word, root);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean search(String word, Node node){
|
private boolean search(String word, Node node) {
|
||||||
if(node == null) return false;
|
if (node == null) return false;
|
||||||
int idx = word.charAt(0) - 'a';
|
if (word.length() == 0) return node.isLeaf;
|
||||||
if(node.child[idx] == null) return false;
|
int index = indexForChar(word.charAt(0));
|
||||||
if(word.length() == 1) return node.child[idx].isLeaf;
|
return search(word.substring(1), node.childs[index]);
|
||||||
return search(word.substring(1), node.child[idx]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean startsWith(String prefix) {
|
public boolean startsWith(String prefix) {
|
||||||
return startWith(prefix, root);
|
return startWith(prefix, root);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean startWith(String prefix, Node node){
|
private boolean startWith(String prefix, Node node) {
|
||||||
if(node == null) return false;
|
if (node == null) return false;
|
||||||
if(prefix.length() == 0) return true;
|
if (prefix.length() == 0) return true;
|
||||||
int idx = prefix.charAt(0) - 'a';
|
int index = indexForChar(prefix.charAt(0));
|
||||||
return startWith(prefix.substring(1), node.child[idx]);
|
return startWith(prefix.substring(1), node.childs[index]);
|
||||||
}
|
}
|
||||||
|
|
||||||
private class Node{
|
private int indexForChar(char c) {
|
||||||
Node[] child = new Node[26];
|
return c - 'a';
|
||||||
boolean isLeaf;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -5612,15 +5619,16 @@ class MapSum {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void insert(String key, Node node, int val) {
|
private void insert(String key, Node node, int val) {
|
||||||
int idx = key.charAt(0) - 'a';
|
if (node == null) return;
|
||||||
if (node.child[idx] == null) {
|
if (key.length() == 0) {
|
||||||
node.child[idx] = new Node();
|
node.value = val;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (key.length() == 1) {
|
int index = indexForChar(key.charAt(0));
|
||||||
node.child[idx].value = val;
|
if (node.child[index] == null) {
|
||||||
} else {
|
node.child[index] = new Node();
|
||||||
insert(key.substring(1), node.child[idx], val);
|
|
||||||
}
|
}
|
||||||
|
insert(key.substring(1), node.child[index], val);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int sum(String prefix) {
|
public int sum(String prefix) {
|
||||||
|
@ -5628,20 +5636,21 @@ class MapSum {
|
||||||
}
|
}
|
||||||
|
|
||||||
private int sum(String prefix, Node node) {
|
private int sum(String prefix, Node node) {
|
||||||
if (node == null) {
|
if (node == null) return 0;
|
||||||
return 0;
|
if (prefix.length() != 0) {
|
||||||
|
int index = indexForChar(prefix.charAt(0));
|
||||||
|
return sum(prefix.substring(1), node.child[index]);
|
||||||
}
|
}
|
||||||
int sum = node.value;
|
int sum = node.value;
|
||||||
if (prefix.length() == 0) {
|
for (Node child : node.child) {
|
||||||
for (Node next : node.child) {
|
sum += sum(prefix, child);
|
||||||
sum += sum(prefix, next);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
int idx = prefix.charAt(0) - 'a';
|
|
||||||
sum = sum(prefix.substring(1), node.child[idx]);
|
|
||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int indexForChar(char c) {
|
||||||
|
return c - 'a';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user