zoukankan      html  css  js  c++  java
  • 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);
    }

    当你看清人们的真相,于是你知道了,你可以忍受孤独
  • 相关阅读:
    SharePoint讨厌“+”吗?
    如何判断文件是否在占用?
    深入浅出SharePoint——设置站点的默认欢迎页面
    深入浅出SharePoint——通过Feature部署Webpart
    VS2008只支持jQuery1.4.1以下版本
    深入浅出SharePoint——Group的常用操作
    深入浅出SharePoint——更新计算列
    深入浅出SharePoint——计算列如何使用Item的ID
    深入浅出SharePoint——在VS2008中正确定义Webpart并通过feature来部署
    理解Javascript_10_对象模型
  • 原文地址:https://www.cnblogs.com/niuyourou/p/14388963.html
Copyright © 2011-2022 走看看