前缀树(Java,附上通用版本和本题版本)

子串判断

http://www.nowcoder.com/questionTerminal/917a800d4de1423394827932f4725c68

本题 增加了一个has方法 改了startswith方法

import java.util.*;
public class Substr {
    public static class Trie {
        private final int ALPHABET_SIZE = 26;
        private Trie[] children = new Trie[ALPHABET_SIZE];
        boolean isEndOfWord = false;
        public Trie() {}

        public void insert(String word) {
            Trie tmp = this;
            for (char i : word.toCharArray()) {
                if (tmp.children[i-'a'] == null) {
                    tmp.children[i-'a'] = new Trie();
                }
                tmp = tmp.children[i-'a'];
            }
            tmp.isEndOfWord = true;
        }

        public boolean search(String word) {
            Trie tmp = this;
            for (char i : word.toCharArray()) {
                if (tmp.children[i-'a'] == null) {
                    return false;
                }
                tmp = tmp.children[i-'a'];
            }
            return tmp.isEndOfWord ? true : false;
        }

        public boolean startsWith(Trie tmp,String prefix) {
            for (char i : prefix.toCharArray()) {
                if (tmp.children[i-'a'] == null) {
                    return false;
                }
                tmp = tmp.children[i-'a'];
            }
            return true;
        }

        public boolean has(Trie tmp,String word){
            if(tmp.isEndOfWord == true) return false;
            Character c = word.charAt(0);
            boolean a = false;
            if(tmp.children[c - 'a'] != null){
                a = startsWith(tmp,word);
            }
            int temp = 0;
            for (int i = 0; i < 26; i++) {
                if(tmp.children[i] != null){
                    temp = i;
                    break;
                }
            }
            return a||has(tmp.children[temp],word);
        }
    }
    public boolean[] chkSubStr(String[] p, int n, String s) {
        // write code here
        Trie trie = new Trie();
        trie.insert(s);
        boolean[] res = new boolean[n];
        for (int i = 0; i < n; i++) {
            if(trie.has(trie,p[i])) res[i] = true;
        }
        return res;
    }
}

通用版本,实现了insert,search,startswith

public class Trie {
    private final int ALPHABET_SIZE = 26;
    private Trie[] children = new Trie[ALPHABET_SIZE];
    boolean isEndOfWord = false;
    public Trie() {}

    public void insert(String word) {
        Trie tmp = this;
        for (char i : word.toCharArray()) {
            if (tmp.children[i-'a'] == null) {
                tmp.children[i-'a'] = new Trie();
            }
            tmp = tmp.children[i-'a'];
        }
        tmp.isEndOfWord = true;
    }

    public boolean search(String word) {
        Trie tmp = this;
        for (char i : word.toCharArray()) {
            if (tmp.children[i-'a'] == null) {
                return false;
            }
            tmp = tmp.children[i-'a'];
        }
        return tmp.isEndOfWord ? true : false;
    }

    public boolean startsWith(String prefix) {
        Trie tmp = this;
        for (char i : prefix.toCharArray()) {
            if (tmp.children[i-'a'] == null) {
                return false;
            }
            tmp = tmp.children[i-'a'];
        }
        return true;
    }
}
全部评论

相关推荐

05-29 19:11
已编辑
北方民族大学 Java
😭😭😭😭本人26届双非本,后端选手。从25年秋招开始,一直到春招5月份,一共面了12次字节。可以说后面能继续投递面上字节大概率是因为前面一直累计的面评还不错,但是最终的结果往往不尽如人意,黄梁一梦。timeline:如标题,总共面了12次字节,4个不同的岗位。第一次:抖音生活服务测开二面完排序挂第二次:TikTok国际化电商测开三面完排序挂第三次:飞书后端安全团队三面完挂第四次:飞书后端偏基架团队三面完过,HR面完之后询问综合排序不推进。我知道像BAT这样的公司,双非本想拿到一张入场券有多难,也知道每次挂在排序/三面/HR面,那种差一步上岸又被打回原点的落差感有多磨人。可是最后一次字节的这个岗位,已经是5月中旬才开始面得了,春招末期的岗位,我本以为真的缺人,三面过的那天,我真的以为就差一步hr面就稳了,但是,最终的结果很遗憾,综合排序综合排序,不推进了。如果是技术能力的问题,我想也不会每一轮技术面给我通过。思来想去。难道真的就是因为我们双非有案底,所以最后的一切又算什么呢。付出这么多的时间精力,还是抵不过双非学历太差吗?既然如此一开始直接卡掉简历不用给面试不就行了嘛,每一轮面试都给我们生的希望,最后的最后又回到了那个必输的起点。12次字节,说不遗憾是假的,也无数次怀疑过自己:是不是我算法刷得还不够?是不是项目亮点讲得不够好?是不是学历就是一道跨不过去的坎?但回头看,这一年的秋招到春招,从面对面试官紧张到说话卡壳,到后来的从容面对,再到如今甚至能和面试官探讨AI&amp;大模型技术的一些方案思路,我已经比去年的自己强太多了。可能字节于我,真的是一场盛大的单恋,拼尽全力奔赴,却还是没能收到想要的回应。前路漫漫,字节的梦碎了,但我的路还在继续,希望下一站,会有属于我的一场徐风。
不愿吃饼的山羊很友好:你的心理素质是真的强大,如果是我碰到这样都会疯了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务