Leetcode 208 实现 Trie

  JAVA 实现:

复制代码
class Trie {
        private Node head;

        /**
         * Initialize your data structure here.
         */
        public Trie() {
            this.head = new Node();
        }

        /**
         * Inserts a word into the trie.
         */
        public void insert(String word) {
            Node node = head;
            for (int i = 0; i < word.length(); i++) {
                int point = word.charAt(i) - 'a';
                if (node.childs[point] == null) node.childs[point] = new Node();
                else node.childs[point].num++;
                node = node.childs[point];
            }
            node.isEnd = true;
        }

        /**
         * Returns if the word is in the trie.
         */
        public boolean search(String word) {
            Node node = searchLast(word);
            if (node == null) return false;
            return node.isEnd;
        }

        private Node searchLast(String word) {
            Node node = head;
            for (int i = 0; i < word.length(); i++) {
                int point = word.charAt(i) - 'a';
                if (node.childs[point] == null) return null;
                node = node.childs[point];
            }
            return node;
        }

        /**
         * Returns if there is any word in the trie that starts with the given prefix.
         */
        public boolean startsWith(String prefix) {
            Node node = searchLast(prefix);
            return node == null ? false : true;
        }

        private class Node {
            int num;
            boolean isEnd;
            Node[] childs;

            Node() {
                this.num = 1;
                this.isEnd = false;
                this.childs = new Node[26];
            }
        }
    }
复制代码

  JS 实现:

复制代码
/**
 * Initialize your data structure here.
 */
var Trie = function () {
    this.head = new Node();
};

var Node = function () {
    this.isEnd = false;
    this.childs = new Array(26);
    this.nums = 1;
}

/**
 * Inserts a word into the trie.
 * @param {string} word
 * @return {void}
 */
Trie.prototype.insert = function (word) {
    let node = this.head;
    for (let i = 0; i < word.length; i++) {
        let ch = word.charAt(i), chPoint = ch.charCodeAt() - 97;
        if (!node.childs[chPoint]) node.childs[chPoint] = new Node();
        else node.childs[chPoint].nums++;
        node = node.childs[chPoint];
    }
    node.isEnd = true;
};

/**
 * Returns if the word is in the trie.
 * @param {string} word
 * @return {boolean}
 */
Trie.prototype.search = function (word) {
    let node = this.searchLast(word);
    if (!node) return false;
    return node.isEnd;
};

Trie.prototype.searchLast = function (word) {
    let node = this.head;
    for (let i = 0; i < word.length; i++) {
        let ch = word.charAt(i), chPoint = ch.charCodeAt() - 97;
        if (!node.childs[chPoint]) return null;
        node = node.childs[chPoint];
    }
    return node;
}

/**
 * Returns if there is any word in the trie that starts with the given prefix.
 * @param {string} prefix
 * @return {boolean}
 */
Trie.prototype.startsWith = function (prefix) {
    let node = this.searchLast(prefix);
    return !node ? false : true;
};

/**
 * Your Trie object will be instantiated and called as such:
 * var obj = new Trie()
 * obj.insert(word)
 * var param_2 = obj.search(word)
 * var param_3 = obj.startsWith(prefix)
 */
复制代码

 

posted @   牛有肉  阅读(94)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示