当前位置:网站首页>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;
}
}边栏推荐
- 【obs】win-capture需要winrt
- Common validation comments
- IO stream file
- 2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
- pytest+allure+jenkins環境--填坑完畢
- 快速使用 Jacoco 代码覆盖率统计
- Explore Cassandra's decentralized distributed architecture
- vus.SSR在asynData函数中请求数据的注意事项
- Leanote private cloud note building
- SQL优化的魅力!从 30248s 到 0.001s
猜你喜欢

【p2p】本地抓包
![[webrtc] m98 Screen and Window Collection](/img/b1/1ca13b6d3fdbf18ff5205ed5584eef.png)
[webrtc] m98 Screen and Window Collection

2022茶艺师(初级)考试题模拟考试题库及在线模拟考试

2022 tea master (intermediate) examination questions and mock examination

Most elements

2022-07-06: will the following go language codes be panic? A: Meeting; B: No. package main import “C“ func main() { var ch chan struct

MySQL multi column index (composite index) features and usage scenarios
![[UTCTF2020]file header](/img/e3/818e2d531a06ab90de189055f634ad.png)
[UTCTF2020]file header

Shell 脚本的替换功能实现

Why should we understand the trend of spot gold?
随机推荐
Button wizard collection learning - mineral medicine collection and running map
After the interview, the interviewer roast in the circle of friends
IPv4 exercises
Figure out the working principle of gpt3
Button wizard script learning - about tmall grabbing red envelopes
Leetcode 43 String multiplication (2022.02.12)
[unity] several ideas about circular motion of objects
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
numpy中dot函数使用与解析
Gslx680 touch screen driver source code analysis (gslx680. C)
[VHDL parallel statement execution]
C语言通信行程卡后台系统
C语言队列
[experience sharing] how to expand the cloud service icon for Visio
【经验分享】如何为visio扩展云服务图标
Qt学习28 主窗口中的工具栏
Detailed explanation of uboot image generation process of Hisilicon chip (hi3516dv300)
3D reconstruction - stereo correction
Numbers that appear only once
解决问题:Unable to connect to Redis