Skip to content

Commit eff2bfe

Browse files
authored
update: 添加问题“2360.图中的最长环”的代码和题解(#849)
* readme: rm 虚空链接 Signed-off-by: LetMeFly666 <[email protected]> * words: en words Signed-off-by: LetMeFly666 <[email protected]> * 2360: WA.cpp Signed-off-by: LetMeFly666 <[email protected]> * 2360: WA.cpp Signed-off-by: LetMeFly666 <[email protected]> * 2360: WA.cpp Signed-off-by: LetMeFly666 <[email protected]> * 2360: WA.cpp Signed-off-by: LetMeFly666 <[email protected]> * 2360: debug.cpp 0-2-3-1 第一次遍历每个都被赋值了 第二次遍历到3时会直接cnt-上次遍历的3 Signed-off-by: LetMeFly666 <[email protected]> * 2360: WA.cpp Signed-off-by: LetMeFly666 <[email protected]> * 2360: WA.cpp Signed-off-by: LetMeFly666 <[email protected]> * 2360: WA.cpp | [5,2,3,1,-1,-1] 3|-1 Signed-off-by: LetMeFly666 <[email protected]> * 2360: debug.cpp C++和Python不一样,for循环中改变i了是能作用到条件括号中的 Signed-off-by: LetMeFly666 <[email protected]> * 2360: WA.cpp 52行应该为x=edges[x] Signed-off-by: LetMeFly666 <[email protected]> * 2360: AC.cpp Signed-off-by: LetMeFly666 <[email protected]> * 2360: AC.Py Signed-off-by: LetMeFly666 <[email protected]> * 2360: WA.java Signed-off-by: LetMeFly666 <[email protected]> * 2360: AC.java Signed-off-by: LetMeFly666 <[email protected]> * 2360: RE.go panic: runtime error: index out of range [5] with length 5 Signed-off-by: LetMeFly666 <[email protected]> * 2360: AC.go - AC,35.29%,100.00% Signed-off-by: LetMeFly666 <[email protected]> * test: go.for Signed-off-by: LetMeFly666 <[email protected]> * update: 添加问题“2360.图中的最长环”的代码和题解(#849) readme: rm 虚空链接 words: en words 2360: AC.(cpp/go/java/py) + 题解 2360: debug.cpp 0-2-3-1 第一次遍历每个都被赋值了 第二次遍历到3时会直接cnt-上次遍历的3 2360: debug.cpp C++和Python不一样,for循环中改变i了是能作用到条件括号中的 2360: WA.cpp 52行应该为x=edges[x] https://github.com/LetMeFly666/LeetCode/blob/9d5710758ab3cf941c4d0de301af7810f54b10d7/Codes/2360-longest-cycle-in-a-graph.cpp#L52 Signed-off-by: LetMeFly666 <[email protected]> --------- Signed-off-by: LetMeFly666 <[email protected]>
1 parent 93c20b8 commit eff2bfe

10 files changed

+470
-31
lines changed

.commitmsg

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
readme: rm 虚空链接
2+
3+
words: en words
4+
5+
2360: AC.(cpp/go/java/py) + 题解
6+
7+
2360: debug.cpp
8+
0-2-3-1
9+
第一次遍历每个都被赋值了
10+
第二次遍历到3时会直接cnt-上次遍历的3
11+
12+
2360: debug.cpp
13+
C++和Python不一样,for循环中改变i了是能作用到条件括号中的
14+
15+
2360: WA.cpp
16+
52行应该为x=edges[x]
17+
https://github.com/LetMeFly666/LeetCode/blob/9d5710758ab3cf941c4d0de301af7810f54b10d7/Codes/2360-longest-cycle-in-a-graph.cpp#L52
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-29 10:47:24
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-29 11:47:35
6+
* @Description: AC,93.64%,86.13%
7+
*/
8+
#ifdef _WIN32
9+
#include "_[1,2]toVector.h"
10+
#endif
11+
12+
/*
13+
[3,3,4,2,3]
14+
0 1 2 3 4
15+
16+
1
17+
18+
0-----> 3 <----- 4
19+
| ↑
20+
↓ |
21+
2 -------+
22+
*/
23+
24+
/*
25+
0 1 2 3
26+
[2,-1,3,1]
27+
28+
0-->2-->3-->1
29+
30+
*/
31+
32+
/*
33+
0 1 2 3 4 5
34+
[5,2,3,1,-1,-1]
35+
36+
37+
0-->5 4
38+
39+
1---->2
40+
↖ ↙
41+
3
42+
*/
43+
class Solution {
44+
public:
45+
int longestCycle(vector<int>& edges) {
46+
int ans = -1;
47+
int cnt = 1;
48+
vector<int> visited(edges.size());
49+
for (int i = 0; i < edges.size(); i++) {
50+
int begin = cnt, x = i;
51+
while (edges[x] != -1 && !visited[x]) {
52+
visited[x] = cnt++;
53+
x = edges[x];
54+
}
55+
if (edges[x] != -1 && visited[x] >= begin) {
56+
ans = max(ans, cnt - visited[x]);
57+
}
58+
}
59+
return ans;
60+
}
61+
};
62+
63+
64+
#ifdef _WIN32
65+
int main() {
66+
string s;
67+
while (cin >> s) {
68+
vector<int> v = stringToVector(s);
69+
Solution sol;
70+
cout << sol.longestCycle(v) << endl;
71+
}
72+
return 0;
73+
}
74+
#endif
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-29 14:16:26
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-29 14:18:33
6+
*/
7+
package main
8+
9+
func longestCycle(edges []int) int {
10+
ans := -1
11+
cnt := 1
12+
visited := make([]int, len(edges))
13+
for i := range edges {
14+
begin := cnt
15+
for edges[i] != -1 && visited[i] == 0 {
16+
visited[i] = cnt
17+
cnt++
18+
i = edges[i]
19+
}
20+
if edges[i] != -1 && visited[i] >= begin {
21+
ans = max(ans, cnt - visited[i])
22+
}
23+
}
24+
return ans
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-03-29 14:10:15
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-03-29 14:15:56
6+
*/
7+
class Solution {
8+
public int longestCycle(int[] edges) {
9+
int ans = -1;
10+
int cnt = 1;
11+
int[] visited = new int[edges.length];
12+
for (int i = 0; i < edges.length; i++) {
13+
int begin = cnt;
14+
int x = i;
15+
while (edges[x] != -1 && visited[x] == 0) {
16+
visited[x] = cnt++;
17+
x = edges[x];
18+
}
19+
if (edges[x] != -1 && visited[x] >= begin) {
20+
ans = Math.max(ans, cnt - visited[x]);
21+
}
22+
}
23+
return ans;
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
Author: LetMeFly
3+
Date: 2025-03-29 14:06:24
4+
LastEditors: LetMeFly.xyz
5+
LastEditTime: 2025-03-29 14:09:27
6+
'''
7+
from typing import List
8+
9+
class Solution:
10+
def longestCycle(self, edges: List[int]) -> int:
11+
ans = -1
12+
cnt = 1
13+
visited = [0] * len(edges)
14+
for i in range(len(edges)):
15+
begin = cnt
16+
while edges[i] != -1 and not visited[i]:
17+
visited[i] = cnt
18+
cnt += 1
19+
i = edges[i]
20+
if edges[i] != -1 and visited[i] >= begin:
21+
ans = max(ans, cnt - visited[i])
22+
return ans

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @Author: LetMeFly
33
* @Date: 2022-05-19 18:48:53
44
* @LastEditors: LetMeFly.xyz
5-
* @LastEditTime: 2025-03-25 23:40:35
5+
* @LastEditTime: 2025-03-28 23:17:56
66
-->
77
# LetLeet Blog
88

@@ -711,6 +711,7 @@
711711
|2352.相等行列对|中等|<a href="https://leetcode.cn/problems/equal-row-and-column-pairs/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/06/06/LeetCode%202352.%E7%9B%B8%E7%AD%89%E8%A1%8C%E5%88%97%E5%AF%B9/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/131062292" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/equal-row-and-column-pairs/solutions/2298633/letmefly-2352xiang-deng-xing-lie-dui-sho-1hqy/" target="_blank">LeetCode题解</a>|
712712
|2353.设计食物评分系统|中等|<a href="https://leetcode.cn/problems/design-a-food-rating-system/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/02/28/LeetCode%202353.%E8%AE%BE%E8%AE%A1%E9%A3%9F%E7%89%A9%E8%AF%84%E5%88%86%E7%B3%BB%E7%BB%9F/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/145926792" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/design-a-food-rating-system/solutions/3588914/letmefly-2353she-ji-shi-wu-ping-fen-xi-t-5gyt/" target="_blank">LeetCode题解</a>|
713713
|2357.使数组中所有元素都等于零|简单|<a href="https://leetcode.cn/problems/make-array-zero-by-subtracting-equal-amounts/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/02/24/LeetCode%202357.%E4%BD%BF%E6%95%B0%E7%BB%84%E4%B8%AD%E6%89%80%E6%9C%89%E5%85%83%E7%B4%A0%E9%83%BD%E7%AD%89%E4%BA%8E%E9%9B%B6/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129193781" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/make-array-zero-by-subtracting-equal-amounts/solutions/2130112/letmefly-2357shi-shu-zu-zhong-suo-you-yu-2p0w/" target="_blank">LeetCode题解</a>|
714+
|2360.图中的最长环|困难|<a href="https://leetcode.cn/problems/longest-cycle-in-a-graph/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/03/29/LeetCode%202360.%E5%9B%BE%E4%B8%AD%E7%9A%84%E6%9C%80%E9%95%BF%E7%8E%AF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/146687134" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/longest-cycle-in-a-graph/solutions/3632508/letmefly-2360tu-zhong-de-zui-chang-huan-y5puj/" target="_blank">LeetCode题解</a>|
714715
|2363.合并相似的物品|简单|<a href="https://leetcode.cn/problems/merge-similar-items/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/02/28/LeetCode%202363.%E5%90%88%E5%B9%B6%E7%9B%B8%E4%BC%BC%E7%9A%84%E7%89%A9%E5%93%81/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129257424" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/merge-similar-items/solutions/2137951/letmefly-2363he-bing-xiang-si-de-wu-pin-gzust/" target="_blank">LeetCode题解</a>|
715716
|2367.算术三元组的数目|简单|<a href="https://leetcode.cn/problems/number-of-arithmetic-triplets/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/31/LeetCode%202367.%E7%AE%97%E6%9C%AF%E4%B8%89%E5%85%83%E7%BB%84%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129872479" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-arithmetic-triplets/solutions/2200543/letmefly-2367suan-zhu-san-yuan-zu-de-shu-oiyn/" target="_blank">LeetCode题解</a>|
716717
|2368.受限条件下可到达节点的数目|中等|<a href="https://leetcode.cn/problems/reachable-nodes-with-restrictions/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2024/03/02/LeetCode%202368.%E5%8F%97%E9%99%90%E6%9D%A1%E4%BB%B6%E4%B8%8B%E5%8F%AF%E5%88%B0%E8%BE%BE%E8%8A%82%E7%82%B9%E7%9A%84%E6%95%B0%E7%9B%AE/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/136418048" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/reachable-nodes-with-restrictions/solutions/2663321/letmefly-2368shou-xian-tiao-jian-xia-ke-5ezsg/" target="_blank">LeetCode题解</a>|
@@ -1015,7 +1016,7 @@
10151016
- [ ] Readme(尤其是文章列表部分)自动生成而非半自动或手动输修改
10161017
- [ ] 有空玩下[这个](https://github.com/LetMeFly666/ViT-MGI/commit/df2255f07aa318d55f44da262315789a15f0f2fc)
10171018
- [ ] arknights主题不支持mermaid的渲染
1018-
- [ ] 研究DQT的[](https://github.com/LetMeFly666/LeetCode/tree/f14c448bc54f4efc3fa41b1d691d5e58a629353f/Codes/1366-rank-teams-by-votes_DQT-RE_version.cpp)[](1366-rank-teams-by-votes_DQT-RE_version.modifing.cpp)为何RE
1019+
- [ ] 研究DQT的[代码](https://github.com/LetMeFly666/LeetCode/tree/f14c448bc54f4efc3fa41b1d691d5e58a629353f/Codes/1366-rank-teams-by-votes_DQT-RE_version.cpp)为何RE
10191020
- [ ] Hexo的`$1\_2$`会被直接渲染成`$1_2$`,然后前端mathjs就会将$1\_2$解析成$1_2$。如[This](https://github.com/LetMeFly666/LeetCode/blob/7a007400b54908796c58576cb587c5f0a99550b8/Solutions/Other-Notes-Mianjing.md?plain=1#L358)
10201021
- hexo我是一刻也待不下去了
10211022
- [x] 生成题解的时候还是按一下回车再开始吧,要不然想要像[这次](https://github.com/LetMeFly666/LeetCode/issues/787)多次边coding边提交,生成题解文件过早还得手动复制代码过去。

0 commit comments

Comments
 (0)