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

目录

前言

习题

1.移除可疑的方法

后言


前言

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


习题

1.移除可疑的方法

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

题面:

**分析:**我们可以定义一个集合数组,map[i]就表示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);
            }
        }
    }
}

后言

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

相关推荐
草莓火锅1 分钟前
用c++使输入的数字各个位上数字反转得到一个新数
开发语言·c++·算法
散峰而望11 分钟前
C/C++输入输出初级(一) (算法竞赛)
c语言·开发语言·c++·算法·github
Kuo-Teng19 分钟前
LeetCode 160: Intersection of Two Linked Lists
java·算法·leetcode·职场和发展
Jooou33 分钟前
Spring事务实现原理深度解析:从源码到架构全面剖析
java·spring·架构·事务
fie888937 分钟前
基于MATLAB的狼群算法实现
开发语言·算法·matlab
偷偷的卷1 小时前
【算法笔记 11】贪心策略六
笔记·算法
盖世英雄酱581361 小时前
commit 成功为什么数据只更新了部分?
java·数据库·后端
ZPC82102 小时前
FPGA 部署ONNX
人工智能·python·算法·机器人
码上淘金2 小时前
在 YAML 中如何将 JSON 对象作为字符串整体赋值?——兼谈 Go Template 中的 fromJson 使用
java·golang·json