diff --git a/solution/2300-2399/2360.Longest Cycle in a Graph/README.md b/solution/2300-2399/2360.Longest Cycle in a Graph/README.md index 408ac99eba2d8..46d35afeff1e7 100644 --- a/solution/2300-2399/2360.Longest Cycle in a Graph/README.md +++ b/solution/2300-2399/2360.Longest Cycle in a Graph/README.md @@ -208,7 +208,7 @@ func longestCycle(edges []int) int { ```ts function longestCycle(edges: number[]): number { const n = edges.length; - const vis = new Array(n).fill(false); + const vis: boolean[] = Array(n).fill(false); let ans = -1; for (let i = 0; i < n; ++i) { if (vis[i]) { @@ -216,15 +216,15 @@ function longestCycle(edges: number[]): number { } let j = i; const cycle: number[] = []; - for (; j != -1 && !vis[j]; j = edges[j]) { + for (; j !== -1 && !vis[j]; j = edges[j]) { vis[j] = true; cycle.push(j); } - if (j == -1) { + if (j === -1) { continue; } for (let k = 0; k < cycle.length; ++k) { - if (cycle[k] == j) { + if (cycle[k] === j) { ans = Math.max(ans, cycle.length - k); break; } @@ -234,6 +234,44 @@ function longestCycle(edges: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn longest_cycle(edges: Vec) -> i32 { + let n = edges.len(); + let mut vis = vec![false; n]; + let mut ans = -1; + + for i in 0..n { + if vis[i] { + continue; + } + let mut j = i as i32; + let mut cycle = Vec::new(); + + while j != -1 && !vis[j as usize] { + vis[j as usize] = true; + cycle.push(j); + j = edges[j as usize]; + } + + if j == -1 { + continue; + } + + for k in 0..cycle.len() { + if cycle[k] == j { + ans = ans.max((cycle.len() - k) as i32); + break; + } + } + } + ans + } +} +``` + diff --git a/solution/2300-2399/2360.Longest Cycle in a Graph/README_EN.md b/solution/2300-2399/2360.Longest Cycle in a Graph/README_EN.md index 3703558937100..41642f1eafd28 100644 --- a/solution/2300-2399/2360.Longest Cycle in a Graph/README_EN.md +++ b/solution/2300-2399/2360.Longest Cycle in a Graph/README_EN.md @@ -202,7 +202,7 @@ func longestCycle(edges []int) int { ```ts function longestCycle(edges: number[]): number { const n = edges.length; - const vis = new Array(n).fill(false); + const vis: boolean[] = Array(n).fill(false); let ans = -1; for (let i = 0; i < n; ++i) { if (vis[i]) { @@ -210,15 +210,15 @@ function longestCycle(edges: number[]): number { } let j = i; const cycle: number[] = []; - for (; j != -1 && !vis[j]; j = edges[j]) { + for (; j !== -1 && !vis[j]; j = edges[j]) { vis[j] = true; cycle.push(j); } - if (j == -1) { + if (j === -1) { continue; } for (let k = 0; k < cycle.length; ++k) { - if (cycle[k] == j) { + if (cycle[k] === j) { ans = Math.max(ans, cycle.length - k); break; } @@ -228,6 +228,44 @@ function longestCycle(edges: number[]): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn longest_cycle(edges: Vec) -> i32 { + let n = edges.len(); + let mut vis = vec![false; n]; + let mut ans = -1; + + for i in 0..n { + if vis[i] { + continue; + } + let mut j = i as i32; + let mut cycle = Vec::new(); + + while j != -1 && !vis[j as usize] { + vis[j as usize] = true; + cycle.push(j); + j = edges[j as usize]; + } + + if j == -1 { + continue; + } + + for k in 0..cycle.len() { + if cycle[k] == j { + ans = ans.max((cycle.len() - k) as i32); + break; + } + } + } + ans + } +} +``` + diff --git a/solution/2300-2399/2360.Longest Cycle in a Graph/Solution.rs b/solution/2300-2399/2360.Longest Cycle in a Graph/Solution.rs new file mode 100644 index 0000000000000..015bc805946c0 --- /dev/null +++ b/solution/2300-2399/2360.Longest Cycle in a Graph/Solution.rs @@ -0,0 +1,33 @@ +impl Solution { + pub fn longest_cycle(edges: Vec) -> i32 { + let n = edges.len(); + let mut vis = vec![false; n]; + let mut ans = -1; + + for i in 0..n { + if vis[i] { + continue; + } + let mut j = i as i32; + let mut cycle = Vec::new(); + + while j != -1 && !vis[j as usize] { + vis[j as usize] = true; + cycle.push(j); + j = edges[j as usize]; + } + + if j == -1 { + continue; + } + + for k in 0..cycle.len() { + if cycle[k] == j { + ans = ans.max((cycle.len() - k) as i32); + break; + } + } + } + ans + } +} diff --git a/solution/2300-2399/2360.Longest Cycle in a Graph/Solution.ts b/solution/2300-2399/2360.Longest Cycle in a Graph/Solution.ts index 55de63d3065e0..f0eacd02241ab 100644 --- a/solution/2300-2399/2360.Longest Cycle in a Graph/Solution.ts +++ b/solution/2300-2399/2360.Longest Cycle in a Graph/Solution.ts @@ -1,6 +1,6 @@ function longestCycle(edges: number[]): number { const n = edges.length; - const vis = new Array(n).fill(false); + const vis: boolean[] = Array(n).fill(false); let ans = -1; for (let i = 0; i < n; ++i) { if (vis[i]) { @@ -8,15 +8,15 @@ function longestCycle(edges: number[]): number { } let j = i; const cycle: number[] = []; - for (; j != -1 && !vis[j]; j = edges[j]) { + for (; j !== -1 && !vis[j]; j = edges[j]) { vis[j] = true; cycle.push(j); } - if (j == -1) { + if (j === -1) { continue; } for (let k = 0; k < cycle.length; ++k) { - if (cycle[k] == j) { + if (cycle[k] === j) { ans = Math.max(ans, cycle.length - k); break; }