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);
    }

    当你看清人们的真相,于是你知道了,你可以忍受孤独
  • 相关阅读:
    2018年10月22日-Python day1
    Python list(列表)功能详解
    剑指offer(4):重建二叉树
    剑指offer(3):从尾到头打印单链表
    剑指offer(2):替换空格
    机器学习实战:第九章 树回归
    ubuntu 中查找文件的命令
    关于 c++ primer plus 中valarray类使用例程的一个记录
    vim 最基本操作
    如何在VS2015中使用strcpy函数
  • 原文地址:https://www.cnblogs.com/niuyourou/p/14388963.html
Copyright © 2011-2022 走看看