当前位置:网站首页>Leetcode - 6135: the longest part of the figure
Leetcode - 6135: the longest part of the figure
2022-08-01 07:58:00 【chrysanthemum bat】
leetcode-6135:The longest part of the figure
题目
题目连接
给你一个 n 个节点的 有向图 ,节点编号为 0 到 n - 1 ,其中每个节点 至多 有一条出边.
图用一个大小为 n 下标从 0 开始的数组 edges 表示,节点 i 到节点 edges[i] 之间有一条有向边.如果节点 i 没有出边,那么 edges[i] == -1 .
请你返回图中的 最长 环,如果没有任何环,请返回 -1 .
一个环指的是起点和终点是 同一个 节点的路径.
示例 1:

输入:edges = [3,3,4,2,3]
输出去:3
解释:The longest part of the figure是:2 -> 4 -> 3 -> 2 .
这个环的长度为 3 ,所以返回 3 .
示例 2:

输入:edges = [2,-1,3,1]
输出:-1
解释:图中没有任何环.
解题
方法一:Within the base ring tree to find ring+时间戳
class Solution {
public:
int longestCycle(vector<int>& edges) {
int n=edges.size();
vector<int> time(n,0);
int res=-1;
for(int i=0,clock=1;i<n;i++){
if(time[i]) continue;
for(int x=i,start_time=clock;x>=0;x=edges[x]){
if(time[x]){
if(time[x]>=start_time){
res=max(res,clock-time[x]);
}
break;
}
time[x]=clock++;
}
}
return res;
}
};
边栏推荐
- How to generate and configure public key certificate in Alipay
- VSCode插件推荐(Rust环境)
- navicat mysql 内存占用过高,被强制关闭
- Data Analysis 6
- How to use Photoshop to composite star trail photos, post-processing method of night sky star trail photos
- 升级为重量级锁,锁重入会导致锁释放?
- R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:gtExtras包的pad_fn函数与gt::fmt函数一起用于填充包含数值的特定列、对数据列的数值进行十进制对齐(从小数点对齐)
- GO error handling
- mysql查看cpu使用情况
- LabVIEW RT中的用户界面更新速度
猜你喜欢

拳头游戏免版权音乐下载,英雄联盟无版权音乐,可用于视频创作、直播

HoloView -- Tabular Datasets

【杭电多校第四场 B题】最短路图+缩点dp

【HDLBits 刷题】Circuits(1)Combinational Logic

配置我的kitty

How to use Photoshop to composite star trail photos, post-processing method of night sky star trail photos

VoLTE基础学习系列 | 企业语音网简述

rhcsa 第三次

Fist game copyright-free music download, League of Legends copyright-free music, can be used for video creation, live broadcast

云原生FAQ
随机推荐
云原生FAQ
Chapters 6 and 7 of Huawei Deep Learning Course
【手撕AHB-APB Bridge】~ AHB地址总线的低两位为什么不用来表示地址呢?
JVM: Runtime Data Area - PC Register (Program Counter)
【一句话攻略】彻底理解JS中的回调(Callback)函数
Mysql数据库的部署以及初始化步骤
22 Grab the Seat 1 C.Grab the Seat (Geometry + Violence)
最小生成树
电磁兼容简明教程(6)测试项目
华为深度学习课程第六、七章
插入排序—直接插入排序和希尔排序
HoloView 在 jyputer lab/notebook 不显示总结
22牛客多校1 J.Serval and Essay (启发式合并)
VSCode 快捷键及通用插件推荐
配置我的kitty
Generate pictures based on the content of the specified area and share them with a summary
七夕来袭——属于程序员的浪漫
Case practice --- Resnet classic convolutional neural network (Mindspore)
VSCode插件推荐(Rust环境)
Redis 3.2.3 crashed by signal: 11 服务宕机问题排查
