当前位置:网站首页>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;
}
}
边栏推荐
- SQL优化的魅力!从 30248s 到 0.001s
- Live broadcast platform source code, foldable menu bar
- Cnopendata American Golden Globe Award winning data
- Pytest+allure+jenkins environment -- completion of pit filling
- [guess-ctf2019] fake compressed packets
- 快速使用 Jacoco 代码覆盖率统计
- Zhilian + AV, AITO asked M7 to do more than ideal one
- [unity] several ideas about circular motion of objects
- Custom class loader loads network class
- Hands on deep learning (IV) -- convolutional neural network CNN
猜你喜欢
[2022 ciscn] replay of preliminary web topics
Cnopendata list data of Chinese colleges and Universities
Idea add class annotation template and method template
What are the positions of communication equipment manufacturers?
A bit of knowledge - about Apple Certified MFI
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
Linux server development, SQL statements, indexes, views, stored procedures, triggers
SQL优化的魅力!从 30248s 到 0.001s
有 Docker 谁还在自己本地安装 Mysql ?
2022 tea master (intermediate) examination questions and mock examination
随机推荐
Button wizard script learning - about tmall grabbing red envelopes
buuctf misc USB
Linux server development, SQL statements, indexes, views, stored procedures, triggers
Qt学习26 布局管理综合实例
[advanced digital IC Verification] command query method and common command interpretation of VCs tool
leanote私有云笔记搭建
[webrtc] M98 screen and window acquisition
Info | webrtc M97 update
Pytest+allure+jenkins installation problem: pytest: error: unrecognized arguments: --alluredir
C语言通信行程卡后台系统
Tongda injection 0day
Operation suggestions for today's spot Silver
2022茶艺师(初级)考试题模拟考试题库及在线模拟考试
padavan手动安装php
知识点滴 - 关于苹果认证MFI
快速使用 Jacoco 代码覆盖率统计
The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)
Ansible
Technology cloud report: from robot to Cobot, human-computer integration is creating an era
Button wizard collection learning - mineral medicine collection and running map