当前位置:网站首页>2022.7.23-----leetcode.剑指offer.115
2022.7.23-----leetcode.剑指offer.115
2022-07-26 11:57:00 【路Lu727】
int V;//顶点数量
Deque<Integer>[] adj;//邻接表
int[] in;//存储每个顶点的入度
//添加一条边
public void addEdge(int v,int w){
if(!adj[v].contains(w)){
adj[v].offer(w);
in[w]++;
}
}
public boolean sequenceReconstruction(int[] nums, int[][] ss) {
V = nums.length;
in=new int[V+1];
adj = new ArrayDeque[V+1];
for (int i = 0; i < adj.length; i++) {
adj[i]=new ArrayDeque<>();
}
//建图
for(int[] s:ss){
if(s.length==1)
continue;
for(int i=1;i<s.length;i++)
addEdge(s[i-1],s[i]);
}
return topologicalSort1();
}
public boolean topologicalSort1(){
Deque<Integer> q=new ArrayDeque<>();
//将入度为0的顶点加入队列
for (int i = 1; i <= V; i++) {
if(in[i]==0)
q.add(i);
}
//如果nums唯一,每次q中只能有一个顶点
if(q.size()>1)
return false;
while(!q.isEmpty()){
int v=q.poll();//取出入度为0的顶点
for (int w:adj[v]) {
in[w]--;//v指向的顶点入度减1
if(in[w]==0)
q.add(w);
}
if(q.size()>1)
return false;
}
return true;//如果能确定包含[1,n]的唯一的序列,必为最短
}边栏推荐
- Redis database, which can be understood by zero foundation Xiaobai, is easy to learn and use!
- 常用的 list.isEmpty() 为何突然报null?
- Hou Peixin, chairman of the openharmony Working Committee of the open atom open source foundation, sent a message to the openatom openharmony sub forum
- pytest接口自动化测试框架 | pytest获取执行数据、pytest禁用插件
- SSJ-21B时间继电器
- 三维点云课程(八)——特征点匹配
- [communication principle] Chapter 3 -- random process [i]
- Pytest interface automation test framework | setup and teardown functions of pytest
- Pytest interface automated test framework | fixture call fixture
- What is per title encoding?
猜你喜欢

The difference between JVM memory overflow and memory leak
![[countdown 10 days] Tencent cloud audio and video special is about to meet, and the thousand yuan prize is waiting for you!](/img/a0/4910970a089cab198875944c7ae88c.png)
[countdown 10 days] Tencent cloud audio and video special is about to meet, and the thousand yuan prize is waiting for you!

。。。。。。

Use the jsonobject object in fastjason to simplify post request parameter passing

RFID的工作原理

专访即构科技李凯:音视频的有趣、行业前沿一直吸引着我

3D point cloud course (VIII) -- feature point matching

种种迹象表明,Apple将有望支持AV1

Some practical, commonly used and increasingly efficient kubernetes aliases

Harbor2.2 quick check of user role permissions
随机推荐
Sword finger offer 25. merge two sorted linked lists
Sunflower senior product director technology sharing: how to apply in AD domain environment
GA-RPN:引导锚点的建议区域网络
Pytest interface automation test framework | setup and teardown functions of pytest
What is per title encoding?
System call capture and analysis conclusion making system call log collection system
Flink 在 讯飞 AI 营销业务的实时数据分析实践
Cohere博客:在生产环境中运行大型语言模型-推理框架概览
程序员培训学习后好找工作吗?
虚拟偶像代言产品出问题谁负责? 且听律师分析
Pytest interface automation test framework | rerun failed cases
Ubenwa, a start-up under Mila, received an investment of US $2.5 million to study the AI diagnosis of infant health
尤雨溪向初学者推荐Vite 【为什么使用Vite】
Audio and video+
DS-112时间继电器
System call capture and analysis - modify kernel methods to add system calls
Mongodn database is connected in the form of URL
System call capture and analysis - ring layer kprobe hijacks system calls
Recalling Sister Feng
RFID的工作原理