当前位置:网站首页>Li Kou interview question 04.01 Path between nodes
Li Kou interview question 04.01 Path between nodes
2022-07-07 07:55:00 【Yangshiwei....】
subject :
analysis :
This can be done in the form of establishing adjacency tables , Use one hashmap Store which nodes each node can point to , And then start Put in queue , Find out the correspondence key The node that can be reached and has not been visited in the value , Put it in the queue and set the value of the corresponding Boolean array to true, If the queue is empty, there is no path .
Code :
class Solution {
public boolean findWhetherExistsPath(int n, int[][] graph, int start, int target) {
boolean[] b=new boolean[n];
HashMap<Integer,List<Integer>> hhash=new HashMap<Integer,List<Integer>>();
Queue<Integer> q=new LinkedList();
for(int i=0;i<graph.length;i++){
if(!hhash.containsKey(graph[i][0])){
hhash.put(graph[i][0],new ArrayList());
}
List list=hhash.get(graph[i][0]);
list.add(graph[i][1]);
}
q.offer(start);
b[start]=true;
while(!q.isEmpty()){
int s=q.poll();
if(!hhash.containsKey(s)){
continue;
}else{
List list=hhash.get(s);
for(int j=0;j<list.size();j++){
int i=(int)list.get(j);
if(!b[i]){
if(i==target){
return true;
}else{
b[i]=true;
q.offer(i);
}
}
}
}
}
return false;
}
}
边栏推荐
猜你喜欢
LeetCode 90:子集 II
Shell 脚本的替换功能实现
misc ez_ usb
【数学笔记】弧度
[webrtc] m98 Screen and Window Collection
2022 welder (elementary) judgment questions and online simulation examination
You Li takes you to talk about C language 6 (common keywords)
[Stanford Jiwang cs144 project] lab3: tcpsender
2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation
[experience sharing] how to expand the cloud service icon for Visio
随机推荐
[unity] several ideas about circular motion of objects
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
Live broadcast platform source code, foldable menu bar
Cnopendata list data of Chinese colleges and Universities
[SUCTF 2019]Game
为什么要了解现货黄金走势?
图解GPT3的工作原理
C语言通信行程卡后台系统
知识点滴 - 关于苹果认证MFI
pytest+allure+jenkins环境--填坑完毕
通信设备商,到底有哪些岗位?
解决could not find or load the Qt platform plugin “xcb“in ““.
【斯坦福计网CS144项目】Lab4: TCPConnection
Qt学习28 主窗口中的工具栏
Technology cloud report: from robot to Cobot, human-computer integration is creating an era
解决:Could NOT find KF5 (missing: CoreAddons DBusAddons DocTools XmlGui)
面试结束后,被面试官在朋友圈吐槽了......
Linux server development, SQL statements, indexes, views, stored procedures, triggers
微信小程序中使用wx.showToast()进行界面交互
Jenkins remote build project timeout problem