54、图论-实现Trie前缀树

 思路:

主要是构建一个trie前缀树结构。如果构建呢?看题意,应该当前节点对象下有几个属性:

1、next节点数组

2、是否为结尾

3、当前值

代码如下:

class Trie {

    class Node {
        boolean end;
        Node[] nexts;

        public Node() {
            end = false;
            nexts = new Node[26];
        }
    }

    public Node root;

    public Trie() {
        root = new Node();
    }

    public void insert(String word) {
        if (word == null) {
            return;
        }
        char[] chs = word.toCharArray();
        Node node = root;
        int path;
        for (int i = 0; i < chs.length; i++) {
            path = chs[i] - 'a';
            if (node.nexts[path] == null) {
                node.nexts[path] = new Node();
            }
            node = node.nexts[path];
        }
        node.end = true;
    }

    public boolean search(String word) {
        if (word == null) {
            return false;
        }
        char[] chs = word.toCharArray();
        Node node = root;
        int path;
        for (int i = 0; i < chs.length; i++) {
            path = chs[i] - 'a';
            if (node.nexts[path] == null) {
                return false;
            }
            node = node.nexts[path];
        }
        return node.end;
    }

    public boolean startsWith(String prefix) {
        if (prefix == null) {
            return false;
        }
        char[] chs = prefix.toCharArray();
        int path;
        Node node = root;
        for (int i = 0; i < chs.length; i++) {
            path = chs[i] - 'a';
            if (node.nexts[path] == null) {
                return false;
            }
            node = node.nexts[path];
        }
        return true;
    }
}

相关推荐

  1. 【LeetCode热题100】【实现 Trie (前缀)

    2024-04-23 09:42:05       37 阅读
  2. 实现 Trie (前缀)

    2024-04-23 09:42:05       61 阅读
  3. 【字典Trie】LeetCode-208. 实现 Trie (前缀)

    2024-04-23 09:42:05       59 阅读
  4. leetcode 208. 实现 Trie (前缀)

    2024-04-23 09:42:05       55 阅读
  5. 算法训练|实现 Trie (前缀)

    2024-04-23 09:42:05       71 阅读
  6. LeetCode 算法:实现 Trie (前缀) c++

    2024-04-23 09:42:05       26 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-23 09:42:05       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-23 09:42:05       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-23 09:42:05       87 阅读
  4. Python语言-面向对象

    2024-04-23 09:42:05       96 阅读

热门阅读

  1. 【ChatGPT】【Gemini】-用Python调用google的Gemini API

    2024-04-23 09:42:05       38 阅读
  2. 在终端使用DOCKER部署ollama

    2024-04-23 09:42:05       38 阅读
  3. WPF之自定义控件模版

    2024-04-23 09:42:05       31 阅读
  4. 【软件测试】白盒测试White box testing

    2024-04-23 09:42:05       33 阅读
  5. PaddleSeg(1)配置文件详解

    2024-04-23 09:42:05       30 阅读
  6. Mockito

    Mockito

    2024-04-23 09:42:05      27 阅读
  7. 华为od机试真题——找磨损度最高和最低的硬盘

    2024-04-23 09:42:05       32 阅读
  8. oracle11g集群挂起

    2024-04-23 09:42:05       29 阅读
  9. .Net4.0 Web.config 配置实践

    2024-04-23 09:42:05       38 阅读
  10. Apache Spark 的基本概念和在大数据分析中的应用

    2024-04-23 09:42:05       34 阅读