• Leetcode676 实现魔法字典


     

      字典树实现,JAVA:

    class MagicDictionary {
            Node root;
    
            /**
             * Initialize your data structure here.
             */
            public MagicDictionary() {
            }
    
            public void buildDict(String[] dictionary) {
                this.root = new Node();
                for (String s : dictionary) this.insert(this.root, s);
            }
    
            public final boolean search(String searchWord) {
                return search(this.root, searchWord, 0, 0);
            }
    
            public final boolean search(Node node, String searchWord, int errorNum, int point) {
                if (node == null || errorNum == 2) return false;
                if (point == searchWord.length()) return errorNum == 1 && node.isEnd;
                int cPoint = searchWord.charAt(point) - 'a';
                boolean re = false;
                for (int i = 0; i < 26; i++) {
                    if (i == cPoint && node.childs[cPoint] != null)
                        re |= search(node.childs[i], searchWord, errorNum, point + 1);
                    else re |= search(node.childs[i], searchWord, errorNum + 1, point + 1);
                }
                return re;
            }
    
            private void insert(Node root, String str) {
                Node node = root, pre = root;
                for (int i = 0; i < str.length(); i++) {
                    char c = str.charAt(i);
                    int cPoint = c - 'a';
                    if (node.childs[cPoint] == null) node.childs[cPoint] = new Node();
                    node = node.childs[cPoint];
                }
                node.isEnd = true;
            }
    
            private class Node {
                Node[] childs;
                boolean isEnd;
                boolean preEnd;
    
                Node() {
                    this.childs = new Node[26];
                    this.preEnd = this.isEnd = false;
                }
            }
        }

      字典树实现,JS:

    /**
     * Initialize your data structure here.
     */
    var MagicDictionary = function () {
        this.root = new Node();
    };
    
    /**
     * @param {string[]} dictionary
     * @return {void}
     */
    MagicDictionary.prototype.buildDict = function (dictionary) {
        for (let i = 0; i < dictionary.length; i++) insert(this.root, dictionary[i]);
    };
    
    /**
     * @param {string} searchWord
     * @return {boolean}
     */
    MagicDictionary.prototype.search = function (searchWord) {
        return searchHandle(this.root, searchWord, 0, 0);
    };
    
    var searchHandle = function (node, str, strPoint, errorNum) {
        if (errorNum == 2 || !node) return false;
        if (strPoint == str.length) return node.isEnd && errorNum == 1;
        let cPoint = str.charAt(strPoint).charCodeAt() - 97, re = false;
        for (let i = 0; i < 26; i++) {
            if (i == cPoint && node.childs[cPoint]) re |= searchHandle(node.childs[i], str, strPoint + 1, errorNum);
            else re |= searchHandle(node.childs[i], str, strPoint + 1, errorNum + 1);
        }
        return re;
    }
    
    var insert = function (root, str) {
        let node = root;
        for (let i = 0; i < str.length; i++) {
            let cPiont = str.charAt(i).charCodeAt() - 97;
            if (!node.childs[cPiont]) node.childs[cPiont] = new Node();
            node = node.childs[cPiont];
        }
        node.isEnd = true;
    }
    
    var Node = function () {
        this.isEnd = false;
        this.childs = new Array(26);
    }

    当你看清人们的真相,于是你知道了,你可以忍受孤独
  • 相关阅读:
    博客园添加访问人数统计【转】
    Android环境下通过C框架层控制WIFI【转】
    用户态文件系统fuse学习【转】
    linux内核 RCU机制详解【转】
    使用diff制作补丁【学习笔记】
    OAuth2授权原理
    Code! MVC 5 App with Facebook, Twitter, LinkedIn and Google OAuth2 Sign-on (C#)
    lock关键字只不过是C#提供的语法糖
    关于OATUH中的AUTHRAZITON CODE和TOKEN的关系,实际上就是这么回事
    SQL Server 索引设计指南
  • 原文地址:https://www.cnblogs.com/niuyourou/p/14388963.html
Copyright © 2020-2023  润新知