力扣-面试题04.01题 节点间通路(C++)- BFS

力扣-面试题04.01题 节点间通路(C++)- BFS,第1张

题目链接:https://leetcode.cn/problems/route-between-nodes-lcci/
题目如下:

class Solution {
public:
    bool findWhetherExistsPath(int n, vector<vector<int>>& graph, int start, int target) {
        //存储图的各条边
        unordered_map<int,vector<int>> umap;
        for(auto e:graph){
            umap[e[0]].push_back(e[1]);
        }

        //bfs,将起始节点放入队列,依次,获得所有以此为起点的终节点
        queue<int> que;
        que.push(start);

        while(que.size()){
            auto t=que.front();
            que.pop();

            for(auto e:umap[t]){
                que.push(e);
                if(e==target) return true;
            }
        }

        return false;
    }
};

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1296098.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-10
下一篇 2022-06-10

发表评论

登录后才能评论

评论列表(0条)

保存