当前位置:网站首页>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;
}
}
边栏推荐
- [SUCTF 2019]Game
- Button wizard collection learning - mineral medicine collection and running map
- Pytorch parameter initialization
- Leanote private cloud note building
- IO stream file
- 知识点滴 - 关于苹果认证MFI
- [CV] Wu Enda machine learning course notes | Chapter 8
- 【经验分享】如何为visio扩展云服务图标
- Operation suggestions for today's spot Silver
- The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
猜你喜欢
Explore Cassandra's decentralized distributed architecture
Qt学习27 应用程序中的主窗口
json 数据展平pd.json_normalize
[UTCTF2020]file header
[SUCTF 2019]Game
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
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
知识点滴 - 关于苹果认证MFI
padavan手动安装php
随机推荐
Button wizard collection learning - mineral medicine collection and running map
Installing postgresql11 database under centos7
Operation suggestions for today's spot Silver
IPv4 exercises
Linux server development, MySQL stored procedures, functions and triggers
Visualization Document Feb 12 16:42
[2022 ciscn] replay of preliminary web topics
A bit of knowledge - about Apple Certified MFI
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
芯片 设计资料下载
芯片资料 网站 易特创芯
LeetCode 90:子集 II
nacos
Jenkins remote build project timeout problem
Asemi rectifier bridge rs210 parameters, rs210 specifications, rs210 package
Tongda injection 0day
Qt学习28 主窗口中的工具栏
Tianqing sends instructions to bypass the secondary verification
探索干货篇!Apifox 建设思路
Cnopendata American Golden Globe Award winning data