当前位置:网站首页>2022.7.23 ------ leetcode. Jianzhi offer.115
2022.7.23 ------ leetcode. Jianzhi offer.115
2022-07-26 12:12:00 【Lu 727】
int V;// Number of vertices
Deque<Integer>[] adj;// Adjacency list
int[] in;// Store the penetration of each vertex
// Add an edge
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<>();
}
// Drawing
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<>();
// The degree of engagement is 0 The vertices of join the queue
for (int i = 1; i <= V; i++) {
if(in[i]==0)
q.add(i);
}
// If nums only , Every time q There can only be one vertex in
if(q.size()>1)
return false;
while(!q.isEmpty()){
int v=q.poll();// Take out the penetration 0 The summit of
for (int w:adj[v]) {
in[w]--;//v Point to the vertex in degrees minus 1
if(in[w]==0)
q.add(w);
}
if(q.size()>1)
return false;
}
return true;// If you can be sure to include [1,n] The unique sequence of , Must be the shortest
}边栏推荐
- On the construction and management of low code technology in logistics transportation platform
- There are various signs that apple is expected to support AV1
- Sim900a based on STM32 sends short messages in Chinese and English
- Pytoch deep learning quick start tutorial -- mound tutorial notes (I)
- y9000p2022重装win10问题
- [early knowledge of activities] list of recent activities of livevideostack
- Question and answer No. 48: geek appointment - construction path of observable system
- Network protocol: tcp/ip protocol
- 以太网驱动详解之RMII、SMII、GMII、RGMII接口
- Use and optimization of MySQL composite index (multi column index)
猜你喜欢

Acwing727. Diamond pattern
![[early knowledge of activities] list of recent activities of livevideostack](/img/a5/06c13865b7adbd99d43c1a52f3fc4d.png)
[early knowledge of activities] list of recent activities of livevideostack

网络协议:TCP/IP协议

Real time synchronization and conversion of massive data based on Flink CDC

Li Kai: the interesting and cutting-edge audio and video industry has always attracted me

FPGA入门学习(一) - 第一个FPGA工程

三维点云课程(八)——特征点匹配

二、容器_

Network protocol: tcp/ip protocol

Introduction to FPGA (III) - 38 decoder
随机推荐
FPGA入门学习(三)- 38译码器
Pytest interface automated test framework | fixture call fixture
Pytest interface automation test framework | pytest configuration file
Pytest interface automated testing framework | introduction to fixture of pytest
Pytest interface automated testing framework | pytest obtains execution data, and pytest disables plug-ins
Why BGP server is used in sunflower remote control? Automatic optimal route and high-speed transmission across operators
空洞卷积详解(输入输出大小分析)
Pycharm is really strong
Pytest interface automated testing framework | common plug-ins of pytest
pytest接口自动化测试框架 | 通过标记表达式执行用例
干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)
Real time synchronization and conversion of massive data based on Flink CDC
How does the chain store cashier system help shoe stores manage their branches?
物联网设备加密的意义
需不需要上线MES系统,你在纠结什么?
【安徽大学】考研初试复试资料分享
Flutter 学习之路
Understand the string class
pytest接口自动化测试框架 | pytest获取执行数据、pytest禁用插件
Is it easy to find a job after programmer training?