mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-17 16:51:46 +00:00
fix: move problem into category
This commit is contained in:
29
Hard/longest-cycle-in-a-graph/Solution.java
Normal file
29
Hard/longest-cycle-in-a-graph/Solution.java
Normal file
@@ -0,0 +1,29 @@
|
||||
class Solution {
|
||||
public int longestCycle(int[] edges) {
|
||||
int[] visited = new int[edges.length];
|
||||
int max = -1;
|
||||
|
||||
for (int i = 0; i < edges.length; ++i) {
|
||||
if (visited[i] > 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int current = i;
|
||||
while (current != -1 && visited[current] == 0) {
|
||||
visited[current] = i + 1;
|
||||
current = edges[current];
|
||||
}
|
||||
|
||||
if (current != -1 && visited[current] == i + 1) {
|
||||
int cycle = 1;
|
||||
int temp = edges[current];
|
||||
while (temp != current) {
|
||||
++cycle;
|
||||
temp = edges[temp];
|
||||
}
|
||||
max = max > cycle ? max : cycle;
|
||||
}
|
||||
}
|
||||
return max;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user