• 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)
     */

  • 相关阅读:
    魅力惠_百度百科
    新奢侈主义_百度百科
    FastSocket学习笔记~再说客户端与服务端的组成
    在线支付文章索引(支付宝_微信_银联)
    微信JSApi支付~微信支付代理模式的实现(原创)
    爱上MVC~ajax调用分部视图session超时页面跳转问题
    C#~异步编程再续~await与async引起的w3wp.exe崩溃-问题友好的解决
    poj-3895-Cycles of Lanes 简单DFS
    How to search a table in a store proc and open the store proc
    Github-Client(ANDROID)开源之旅(四) ------ 简介Roboguice
  • 原文地址:https://www.cnblogs.com/niuyourou/p/14225823.html
Copyright © 2020-2023  润新知