力扣labuladong——一刷day76

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


图这种数据结构有一些比较特殊的算法,比如二分图判断,有环图无环图的判断,拓扑排序,以及最经典的最小生成树,单源最短路径问题,更难的就是类似网络流这样的问题。

一、力扣797. 所有可能的路径

class Solution {
   
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
   
        dfs(graph,0);
        return res;
    }
    public void dfs(int[][] graph, int cur){
   
        if(cur == graph.length-1){
   
            path.add(cur);
            res.add(new ArrayList<>(path));
            path.remove(path.size()-1);
            return;
        }
        for(int i = 0; i < graph[cur].length; i ++){
   
            path.add(cur);
            dfs(graph, graph[cur][i]);
            path.remove(path.size()-1);
        }
    }
}

二、力扣277.搜寻名人

int findCelebrity(int n) {
   
    if (n == 1) return 0;
    // 将所有候选人装进队列
    LinkedList<Integer> q = new LinkedList<>();
    for (int i = 0; i < n; i++) {
   
        q.addLast(i);
    }
    // 一直排除,直到只剩下一个候选人停止循环
    while (q.size() >= 2) {
   
        // 每次取出两个候选人,排除一个
        int cand = q.removeFirst();
        int other = q.removeFirst();
        if (knows(cand, other) || !knows(other, cand)) {
   
            // cand 不可能是名人,排除,让 other 归队
            q.addFirst(other);
        } else {
   
            // other 不可能是名人,排除,让 cand 归队
            q.addFirst(cand);
        }
    }

    // 现在排除得只剩一个候选人,判断他是否真的是名人
    int cand = q.removeFirst();
    for (int other = 0; other < n; other++) {
   
        if (other == cand) {
   
            continue;
        }
        // 保证其他人都认识 cand,且 cand 不认识任何其他人
        if (!knows(other, cand) || knows(cand, other)) {
   
            return -1;
        }
    }
    // cand 是名人
    return cand;
}

相关推荐

  1. labuladong——day76

    2023-12-22 14:50:05       54 阅读
  2. labuladong——day70

    2023-12-22 14:50:05       41 阅读
  3. labuladong——day74

    2023-12-22 14:50:05       39 阅读
  4. labuladong——day75

    2023-12-22 14:50:05       39 阅读
  5. labuladong——day77

    2023-12-22 14:50:05       33 阅读
  6. labuladong——day78

    2023-12-22 14:50:05       39 阅读
  7. labuladong——day79

    2023-12-22 14:50:05       37 阅读
  8. labuladong——day68

    2023-12-22 14:50:05       39 阅读
  9. labuladong——day67

    2023-12-22 14:50:05       35 阅读
  10. labuladong——day69

    2023-12-22 14:50:05       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-22 14:50:05       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-22 14:50:05       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-22 14:50:05       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-22 14:50:05       20 阅读

热门阅读

  1. [Spark] 读取项目下resources/的文件

    2023-12-22 14:50:05       38 阅读
  2. Web应用代码自动化审计浅谈

    2023-12-22 14:50:05       38 阅读
  3. Dockerfile巩固:阅读解析nginx的Dockerfile

    2023-12-22 14:50:05       36 阅读
  4. 数据库连接问题 - ChatGPT对自身的定位

    2023-12-22 14:50:05       36 阅读
  5. 第二十一章网络通讯

    2023-12-22 14:50:05       30 阅读
  6. Curl多线程https访问,崩溃问题修复

    2023-12-22 14:50:05       47 阅读