0设计数据结构/前缀树中等 LeetCode208. 实现 Trie (前缀树) NC124 字典树的实现

208. 实现 Trie (前缀树)

描述

Trie(发音类似 “try”)或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的键。这一数据结构有相当多的应用情景,例如自动补完和拼写检查。

请你实现 Trie 类:

Trie() 初始化前缀树对象。
void insert(String word) 向前缀树中插入字符串 word 。
boolean search(String word) 如果字符串 word 在前缀树中,返回 true(即,在检索之前已经插入);否则,返回 false 。
boolean startsWith(String prefix) 如果之前已经插入的字符串 word 的前缀之一为 prefix ,返回 true ;否则,返回 false 。

分析

参考题解:【图解算法】模板+变式——带你彻底搞懂字典树(Trie树)
这是一道设计数据结构类的题目,在执行insert,search,startsWith时是需要有操作对象的,这个对象不能是Trie本身,因为在初始化时不能自己new自己,所以操作的对象得是其他的
类。

前缀树是一个多叉树,字符串的每个字符按照由上向下的顺序对应着一个树结点,当一个字符串结束时,最后一个字符对应的树结点的标记句子的属性是true。

class Trie {
    TrieNode root;
    public Trie() {
        root = new TrieNode();
    }
    
    public void insert(String word) {
        TrieNode cur = root;
        for(int i = 0; i < word.length(); i++){
            int j = word.charAt(i) - 'a';
            if(cur.children[j] == null){
                cur.children[j] = new TrieNode();
            }
            cur = cur.children[j];
        }
        cur.isWord = true;
    }
    
    public boolean search(String word) {
        TrieNode cur = root;
        for(int i = 0; i < word.length(); i++){
            int j = word.charAt(i) - 'a';
            if(cur.children[j] == null){
                return false;
            }
            cur = cur.children[j];
        }
        return cur.isWord;
    }
    
    public boolean startsWith(String prefix) {
        TrieNode cur = root;
        for(int i = 0; i < prefix.length(); i++){
            int j = prefix.charAt(i) - 'a';
            if(cur.children[j] == null){
                return false;
            }
            cur = cur.children[j];
        }
        return true;
    }
}

class TrieNode{
    boolean isWord;
    TrieNode[] children;

    TrieNode(){
        isWord = false;
        children = new TrieNode[26];
    }
}

/**
 * Your Trie object will be instantiated and called as such:
 * Trie obj = new Trie();
 * obj.insert(word);
 * boolean param_2 = obj.search(word);
 * boolean param_3 = obj.startsWith(prefix);
 */

NC124 字典树的实现
不懂为什么牛客就是不能通过,在idea上能够通过测试样例。

import java.util.*;
public class Solution {
    TrieNode root = new TrieNode(0,0,new TrieNode[26]);
    public String[] trieU (String[][] operators) {
        List<String> list = new ArrayList<>();
        for(int i = 0; i < operators.length; i++){
            if(operators[i][0] == "1"){
                root.insert(operators[i][1]);
            }
            if(operators[i][0] == "2"){
                root.delete(operators[i][1]);
            }
            if(operators[i][0] == "3"){
                if(root.search(operators[i][1])){
                    list.add("YES");
                }else{
                    list.add("NO");
                }
            }
            if(operators[i][0] == "4"){
                list.add(""+root.prefixNumber(operators[i][1]));
            }
        }
        String[] ans = new String[list.size()];
        int j = 0;
        for(String str : list){
            ans[j++] = str;
        }
        return ans;
    }
    
    
}
class TrieNode{
    int count;
    int isWord;
    TrieNode[] children;
    TrieNode(int count, int isWord, TrieNode[] children){
        this.count = count;
        this.isWord = isWord;
        this.children = new TrieNode[26];
    }
     void insert(String word){
        TrieNode cur = this;
        for(int i = 0; i < word.length(); i++){
            int index = word.charAt(i) - 'a';
            if(cur.children[index] == null){
                cur.children[index] = new TrieNode(0,0,new TrieNode[26]);
            }
            cur = cur.children[index];
            cur.count++;
            
        }
        cur.isWord++;
    }
    
     void delete(String word){
        TrieNode cur = this;
        for(int i = 0; i < word.length(); i++){
            int index = word.charAt(i) - 'a';
            cur = cur.children[index];
            cur.count--;
        }
        cur.isWord--;
    }
    
     boolean search(String word){
        TrieNode cur = this;
        for(int i = 0; i < word.length(); i++){
            int index = word.charAt(i) - 'a';
            if(cur.children[index] == null){
                return false;
            }
            cur = cur.children[index];
        }
        return cur.isWord > 0;
    }
    
     int prefixNumber(String word){
        TrieNode cur = this;
        for(int i = 0; i < word.length(); i++){
            int index = word.charAt(i) - 'a';
            cur = cur.children[index];
        }
        return cur.count;
    }
}
import java.util.*;

public class Solution {
    TrieNode root = new TrieNode(0,0,new TrieNode[26]);
    public String[] trieU (String[][] operators) {
        List<String> list = new ArrayList<>();
        for(int i = 0; i < operators.length; i++){
            if(operators[i][0] == "1"){
                insert(operators[i][1]);
            }
            if(operators[i][0] == "2"){
                delete(operators[i][1]);
            }
            if(operators[i][0] == "3"){
                if(search(operators[i][1])){
                    list.add("YES");
                }else{
                    list.add("NO");
                }
            }
            if(operators[i][0] == "4"){
                list.add(""+prefixNumber(operators[i][1]));
            }
        }
        String[] ans = new String[list.size()];
        int j = 0;
        for(String str : list){
            ans[j++] = str;
        }
        return ans;
    }
    
    public void insert(String word){
        TrieNode cur = root;
        for(int i = 0; i < word.length(); i++){
            int index = word.charAt(i) - 'a';
            if(cur.children[index] == null){
                cur.children[index] = new TrieNode(0,0,new TrieNode[26]);
            }
            cur = cur.children[index];
            cur.count++;
            
        }
        cur.isWord++;
    }
    
    public void delete(String word){
        TrieNode cur = root;
        for(int i = 0; i < word.length(); i++){
            int index = word.charAt(i) - 'a';
            cur = cur.children[index];
            cur.count--;
        }
        cur.isWord--;
    }
    
    public boolean search(String word){
        TrieNode cur = root;
        for(int i = 0; i < word.length(); i++){
            int index = word.charAt(i) - 'a';
            if(cur.children[index] == null){
                return false;
            }
            cur = cur.children[index];
        }
        return cur.isWord > 0;
    }
    
    public int prefixNumber(String word){
        TrieNode cur = root;
        for(int i = 0; i < word.length(); i++){
            int index = word.charAt(i) - 'a';
            cur = cur.children[index];
        }
        return cur.count;
    }
}
class TrieNode{
    int count;
    int isWord;
    TrieNode[] children;
    TrieNode(int count, int isWord, TrieNode[] children){
        this.count = count;
        this.isWord = isWord;
        this.children = new TrieNode[26];
    }
}
```


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值