当前位置:网站首页>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
- 通信设备商,到底有哪些岗位?
- paddlepaddle 29 无模型定义代码下动态修改网络结构(relu变prelu,conv2d变conv3d,2d语义分割模型改为3d语义分割模型)
- 242. Bipartite graph determination
- 微信小程序基本组件使用介绍
- leanote私有云笔记搭建
- 【斯坦福计网CS144项目】Lab4: TCPConnection
- 探索Cassandra的去中心化分布式架构
- [webrtc] M98 screen and window acquisition
- Explore Cassandra's decentralized distributed architecture
猜你喜欢
随机推荐
pytest+allure+jenkins安装问题:pytest: error: unrecognized arguments: --alluredir
IPv4 exercises
Mysql高低版本切换需要修改的配置5-8(此处以aicode为例)
Common validation comments
2022 welder (elementary) judgment questions and online simulation examination
大视频文件的缓冲播放原理以及实现
【斯坦福计网CS144项目】Lab3: TCPSender
Button wizard script learning - about tmall grabbing red envelopes
buuctf misc USB
2022 tea master (intermediate) examination questions and mock examination
【Unity】物体做圆周运动的几个思路
JS get all date or time stamps between two time stamps
解决could not find or load the Qt platform plugin “xcb“in ““.
Linux server development, SQL statements, indexes, views, stored procedures, triggers
[guess-ctf2019] fake compressed packets
Button wizard collection learning - mineral medicine collection and running map
IO stream file
dash plotly
Wechat applet data binding multiple data
[OBS] win capture requires winrt



![[experience sharing] how to expand the cloud service icon for Visio](/img/42/dba9f78f3fb2049dad8b343b0b36e5.png)

![[mathematical notes] radian](/img/43/2af510adb24fe46fc0033d11d60488.jpg)


![[GUET-CTF2019]虚假的压缩包](/img/a2/7da2a789eb49fa0df256ab565d5f0e.png)
