力扣-图论-5【算法学习day.55】

目录

前言

习题

1.移除可疑的方法

后言


前言

###我做这类文章一个重要的目的还是给正在学习的大家提供方向和记录学习过程(例如想要掌握基础用法,该刷哪些题?)我的解析也不会做的非常详细,只会提供思路和一些关键点,力扣上的大佬们的题解质量是非常非常高滴!!!


习题

1.移除可疑的方法

题目链接: 3310. 移除可疑的方法 - 力扣(LeetCode)

题面:

**分析:**我们可以定义一个集合数组,mapi就表示i节点所指向的所有节点,然后递归将所有可疑节点打上标记,然后遍历invocations数组,如果出现第一个元素是不可疑的,第二个元素是可疑的,就直接返回所有元素,否则,就把没打上可疑标记的元素返回。

java 复制代码
class Solution {
    List<Integer>[] map1;
    List<Integer>[] map2;
    int[] flag;
    ArrayList<Integer> no = new ArrayList<>();
    int[] cuc = new int[100000];
    int count = 0;
    public List<Integer> remainingMethods(int n, int k, int[][] invocations) {
        map1 = new List[n];
          flag = new int[n];
        Arrays.setAll(map1,_->new ArrayList<Integer>());
        for(int[] arr : invocations){
            int a = arr[0];
            int b = arr[1];
            // union(a,b);
            map1[a].add(b);
        }
        recurison(k);
        List<Integer> ans = new ArrayList<>();
         for(int[] arr : invocations){
            if(flag[arr[0]]==0&&flag[arr[1]]==1){
                for(int i = 0;i<n;i++)ans.add(i);
                return ans;
            }
        }

        for(int i = 0;i<n;i++){
            if(flag[i]==0)ans.add(i);
        }
        return ans;
    }
    public void recurison(int x){
        flag[x] =1;
        for(int a:map1[x]){
            if(flag[a]==0){
                recurison(a);
            }
        }
    }
}

后言

上面是力扣图论专题,下一篇是其他的习题,希望有所帮助,一同进步,共勉!

相关推荐
January12071 天前
Cassandra CQL 基本命令详解
java
平生不晚1 天前
在 SVG 体系里画一条任意的线
前端·算法
Escalating_xu1 天前
【Linux】线程概念与控制:从地址空间、LWP 到 pthread_create、join、detach 与 NPTL
java·linux·redis
Lam Tang1 天前
APS 系列文章 09
java·代理模式
打工陈1 天前
电路理论知识学习--(4)RC、LC、LR低通滤波对比
学习·智能硬件
evans在进步1 天前
LeetCode 198:打家劫舍——Java 动态规划详解
java·leetcode·动态规划
Hhy_11071 天前
《C++深度解构02》类和对象(上)——定义规范、内存对齐与 this 指针全解
c语言·开发语言·c++·学习·visual studio
2601_963870221 天前
【计算机毕业设计】基于Spring Boot的考研信息交流平台的设计与实现
java·spring boot·后端
我爱cope1 天前
【计算机网络 | 数据链路层3:MAC 地址是什么?它和 IP 地址有什么区别?】
学习·tcp/ip·计算机网络·智能路由器·数据链路层
电子云与长程纠缠1 天前
UE5 Lyra PocketWorld进行3D内容UI预览 - 下
开发语言·学习·游戏·ui·ue5