牛客网:NC124 字典树的实现

链表实现

package com.company.test.字典树的实现;

/**
 * @author xienl
 * @description 字典树的实现
 * @date 2022/8/23
 */

public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();

    }

    public String[] trieU (String[][] operators) {
        // write code here
        int len = 0;
        for (String[] operator : operators) {
            if (operator[0].equals("3") || operator[0].equals("4")){
                len++;
            }
        }
        Trie trie = new Trie();
        String[] res = new String[len];
        int index = 0;
        for (String[] operator : operators) {
            if (operator[0].equals("1")){
                trie.insert(operator[1]);
            }

            if (operator[0].equals("2")){
                trie.delete(operator[1]);
            }

            if (operator[0].equals("3")){
                boolean search = trie.search(operator[1]);
                res[index++] = search ? "YES" : "NO";
            }

            if (operator[0].equals("4")){
                int num = trie.prefixNumber(operator[1]);
                res[index++] = Integer.toString(num);
            }
        }
        return res;
    }
}

/**
 * 前缀树,链表实现
 */
class Trie{

    /**
     * 节点对象
     */
    class TrieNode{
        int num;
        boolean end;
        TrieNode[] child;
       public TrieNode(){
           this.num = 0;
           this.end = false;
           this.child = new TrieNode[26];
        }

    }

        TrieNode root = new TrieNode();

    /**
     * 增加
     * @param word
     */
    void insert(String word){
        TrieNode node = root;
        char[] chars = word.toCharArray();
        for (int i = 0; i < chars.length; i++){
            char c = word.charAt(i);
            if (node.child[c - 'a'] == null){
                node.child[c - 'a'] = new TrieNode();
            }
            node = node.child[ c - 'a'];
            node.num++;
        }
        node.end = true;
    }

    /**
     * 删除
     * @param word
     */
    void delete(String word){
        TrieNode node = root;
        char[] chars = word.toCharArray();
        for (char c : chars){
            node = node.child[c - 'a'];
            node.num--;
        }
        node.end = false;
    }

    /**
     * 查找
     * @param word
     * @return
     */
    boolean search(String word){
        TrieNode node = root;
        char[] chars = word.toCharArray();
        for (char c : chars) {
            if (node.child[c - 'a'] == null){
                return false;
            }
            node = node.child[c - 'a'];
        }
        return node.end;
    }

    int prefixNumber(String pre){
        TrieNode node = root;
        char[] chars = pre.toCharArray();
        for (char c : chars) {
            if (node.child[c - 'a'] == null){
                return 0;
            }
            node = node.child[c - 'a'];
        }
        return node.num;
    }
}

数组实现

package com.company.test.字典树的实现;

/**
 * @author xienl
 * @description 字典数数组实现
 * @date 2022/8/23
 */

public class Solution2 {
    public static void main(String[] args) {
        Solution solution = new Solution();

    }

    public String[] trieU (String[][] operators) {
            // write code here
            int len = 0;
            for (String[] operator : operators) {
                if (operator[0].equals("3") || operator[0].equals("4")){
                    len++;
                }
            }
            Trie2 trie = new Trie2();
            String[] res = new String[len];
            int index = 0;
            for (String[] operator : operators) {
                if (operator[0].equals("1")){
                    trie.insert(operator[1]);
                }

                if (operator[0].equals("2")){
                    trie.delete(operator[1]);
                }

                if (operator[0].equals("3")){
                    boolean search = trie.search(operator[1]);
                    res[index++] = search ? "YES" : "NO";
                }

                if (operator[0].equals("4")){
                    int num = trie.prefixNumber(operator[1]);
                    res[index++] = Integer.toString(num);
                }
            }
            return res;
    }


}

class Trie2 {
    int n = 700000;
    // 字典数
    int[][] tire;
    // 当前行数
    int[] cnt;
    // 是否结尾
    int[] end;
    int index;

    public Trie2(){
        tire = new int[n][26];
        cnt = new int[n];
        end = new int[n];
        index = 0;
    }

    void insert(String word){
        int cur = 0;
        char[] chars = word.toCharArray();
        for (char c : chars) {
            if (tire[cur][c - 'a'] == 0){
                tire[cur][c - 'a'] = ++index;
            }
            cur = tire[cur][c - 'a'];
            cnt[cur]++;
        }
        end[cur]++;
    }

    void delete(String word){
        int cur = 0;
        char[] chars = word.toCharArray();
        for (char c : chars) {
            cur = tire[cur][c - 'a'];
            cnt[cur]--;
        }
        end[cur]--;
    }

    boolean search(String word){
        int cur = 0;
        char[] chars = word.toCharArray();
        for (char c : chars) {
            if (tire[cur][c - 'a'] == 0){
                return false;
            }
            cur = tire[cur][c - 'a'];
        }
        return end[cur] > 0;
    }

    int prefixNumber(String pre){
        int cur = 0;
        char[] chars = pre.toCharArray();
        for (char c : chars) {
            if (tire[cur][c - 'a'] == 0){
                return 0;
            }
            cur = tire[cur][c - 'a'];
        }
        return cnt[cur];
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值