zoukankan      html  css  js  c++  java
  • 《程序员代码面试指南》第五章 字符串问题 字典树(前缀树)的实现

    题目

    字典树(前缀树)的实现
    

    java代码

    package com.lizhouwei.chapter5;
    
    /**
     * @Description: 字典树(前缀树)的实现
     * @Author: lizhouwei
     * @CreateDate: 2018/4/25 21:34
     * @Modify by:
     * @ModifyDate:
     */
    public class Chapter5_23 {
        public TrieNode root;
    
        public Chapter5_23() {
            root = new TrieNode();
        }
    
        public void insert(String str) {
            char[] chars = str.toCharArray();
            TrieNode node = root;
            int index = 0;
            for (int i = 0; i < chars.length; i++) {
                index = chars[i] - 'a';
                if (node.map[index] == null) {
                    node.map[index] = new TrieNode();
                }
                node = node.map[index];
                node.pathNum++;
            }
        }
    
        public void delete(String str) {
            if (!search(str)) {
                return;
            }
            char[] chars = str.toCharArray();
            TrieNode node = root;
            int index = 0;
    
            for (int i = 0; i < chars.length; i++) {
                index = chars[i] - 'a';
                node = node.map[index];
                if (node.pathNum-- == 1) {
                    node.map[index] = null;
                    return;
                }
            }
            node.endNum--;
        }
    
        public boolean search(String str) {
            char[] chars = str.toCharArray();
            TrieNode node = root;
            int index = 0;
            for (int i = 0; i < chars.length; i++) {
                index = chars[i] - 'a';
                node = node.map[index];
                if (node == null) {
                    return false;
                }
            }
            return node.endNum != 0;
        }
    
        public int prefixNumber(String str) {
            char[] chars = str.toCharArray();
            TrieNode node = root;
            int index = 0;
    
            for (int i = 0; i < chars.length; i++) {
                index = chars[i] - 'a';
                node = node.map[index];
                if (node == null) {
                    return 0;
                }
            }
            return node.pathNum;
        }
    
        //测试
        public static void main(String[] args) {
            Chapter5_23 chapter = new Chapter5_23();
            String[] strs = {"abc", "abcd", "abd", "b", "bcd", "efg", "hik"};
            for (String str : strs) {
                chapter.insert(str);
            }
            boolean exist = chapter.search("abc");
            System.out.println("abc是否在字典树中:" + exist);
            System.out.println("字典树中删除abc");
            chapter.delete("abc");
            exist = chapter.search("abc");
            System.out.println("abc是否在字典树中:" + exist);
            int result = chapter.prefixNumber("abc");
            System.out.println("以abc作为前缀的字符串的个数:" + result);
        }
    }
    
    class TrieNode {
        public int pathNum;
        public int endNum;
        public TrieNode[] map;
    
        public TrieNode() {
            pathNum = 0;
            endNum = 0;
            map = new TrieNode[26];
        }
    }
    
    

    结果

  • 相关阅读:
    Update SSM agent to each EC2 via Bat and bash script
    Shell脚本循环读取文件中的每一行
    通过psexec实现远程安装软件包
    Getting Private/Public IP address of EC2 instance using AWS-cli [closed]
    Python to list users in AWS
    BAT script to set local account password never expire
    Difference between Netbios and Host name
    PowerShell官方文档
    powershell for rename server name
    Create a conditional DNS forwarder on our domain.com to Amazon default DNS provider
  • 原文地址:https://www.cnblogs.com/lizhouwei/p/8947640.html
Copyright © 2011-2022 走看看