Leetcode 208,207实现Trie前缀树,课程表

1.题目描述

题目解答

这道题告诉了我们一种特殊的数据结构:前缀树。但其实本质上,他就是一种特殊的二叉树,也就是26叉树。

对于题目中的要求,我们可以将字符串转化为字母,再将字母转化为对应的ASCII码值,在节点内部创建一个数组,里面存放孩子节点的引用,每一个索引的位置都对应一个小写字母的ASCII码,只要哪个索引上有孩子节点的引用,就说明下一个字母存在。

我们可以通过遍历一个字符串,来进行添加。在节点内部添加一个布尔值,用于说明是否为一个单词的结尾,当添加完毕的时候,将当前的节点设置为结束的节点。这样当节点遍历完毕的时候,就可以知道到底是不是一个完整的单词。

java 复制代码
class Trie {
    //以字母的acsii码为下标进行计算
    public class Node{
        Node[] arr  = new Node[26];
        boolean isend = false;
        public Node(){

        }
    }
     Node root = new Node();
    public Trie() {
        
    }
    
    public void insert(String word) {
        Node node = root;
        for(char c : word.toCharArray()){
            int index = c-'a';
            if(node.arr[index]==null){
               node.arr[index] = new Node(); 
            }
            node = node.arr[index];
        }
        node.isend=true;
    }
    
    public boolean search(String word) {
        Node node = root;
        for(char c :word.toCharArray()){
            int index = c-'a';
            if(node.arr[index]!=null){
                node = node.arr[index];
            }else{
                return false;
            }
        }
        return node.isend;
    }
    
    public boolean startsWith(String prefix) {
        Node node = root;
        for(char c:prefix.toCharArray()){
             int index = c-'a';
            if(node.arr[index]!=null){
                node = node.arr[index];
            }else{
                return false;
            }
        }
        return true;
    }
}

2.题目描述

题目解答

java 复制代码
class Solution {
    public boolean canFinish(int numCourses, int[][] prerequisites) {
        //存放每个节点的入度数组
        int[] inarr = new int[numCourses];
        //key为目标节点,value为目标节点的后继
        Map<Integer,List<Integer>> map = new HashMap<>();
        for(int[] rule:prerequisites){
            inarr [rule[0]]++;
            map.putIfAbsent(rule[1],new ArrayList<>());
            map.get(rule[1]).add(rule[0]);
        }
        Deque<Integer> queue = new ArrayDeque<>();
        //先将刚开始所有入度为零的节点入队
        for(int i=0;i<inarr.length;i++){
            if(inarr[i]==0){
                queue.offer(i);
            } 
        }
        while(!queue.isEmpty()){
                int curNode = queue.poll();
                numCourses--;
                //遍历当前节点的后继,将每个后继的入度都减一
                for(int k:map.getOrDefault(curNode,new ArrayList<>())){
                    inarr[k]--;
                    if(inarr[k]==0) queue.offer(k);
                }
            }
 
        return numCourses == 0;

    }
}
相关推荐
黄金龙PLUS8 分钟前
5个1280比特大状态置换算法的设计与分析
算法·网络安全·密码学·哈希算法·同态加密
LOVE️YOU11 分钟前
Python 数据结构的本质:位置、对象引用、Hash 与可变性
数据结构·python·哈希算法
不会就选b14 分钟前
算法日常・每日刷题--<动态规划>1
java·数据结构·算法
晚安code15 分钟前
JVM 内存结构入门:程序计数器、虚拟机栈、本地方法栈与堆的溢出诊断
java·jvm
shehuiyuelaiyuehao20 分钟前
算法54,链表 两数相加
数据结构·算法·链表
自强的小白23 分钟前
Spring事务失效的场景
java·spring
小蒜学长30 分钟前
基于SpringBoot+Vue的小学数学智能出题系统(代码+数据库+LW)
java·数据库·spring boot·后端·智能出题系统
不灭的黄金瞳12335 分钟前
Java继承与多态
java·intellij-idea
Logic10142 分钟前
C语言/数据结构位运算题解:异或XOR找出弹子游戏机中数量为奇数的数字——只出现奇数次
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
147API1 小时前
如何设计“回答、追问、停答”三类蒸馏训练集
人工智能·算法·机器学习