Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 350a49f

Browse files
authored
feat: add solutions to lc problem: No.2360 (doocs#4298)
No.2360.Longest Cycle in a Graph
1 parent b26a557 commit 350a49f

File tree

4 files changed

+121
-12
lines changed

4 files changed

+121
-12
lines changed

solution/2300-2399/2360.Longest Cycle in a Graph/README.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,23 +208,23 @@ func longestCycle(edges []int) int {
208208
```ts
209209
function longestCycle(edges: number[]): number {
210210
const n = edges.length;
211-
const vis = new Array(n).fill(false);
211+
const vis: boolean[] = Array(n).fill(false);
212212
let ans = -1;
213213
for (let i = 0; i < n; ++i) {
214214
if (vis[i]) {
215215
continue;
216216
}
217217
let j = i;
218218
const cycle: number[] = [];
219-
for (; j != -1 && !vis[j]; j = edges[j]) {
219+
for (; j !== -1 && !vis[j]; j = edges[j]) {
220220
vis[j] = true;
221221
cycle.push(j);
222222
}
223-
if (j == -1) {
223+
if (j === -1) {
224224
continue;
225225
}
226226
for (let k = 0; k < cycle.length; ++k) {
227-
if (cycle[k] == j) {
227+
if (cycle[k] === j) {
228228
ans = Math.max(ans, cycle.length - k);
229229
break;
230230
}
@@ -234,6 +234,44 @@ function longestCycle(edges: number[]): number {
234234
}
235235
```
236236

237+
#### Rust
238+
239+
```rust
240+
impl Solution {
241+
pub fn longest_cycle(edges: Vec<i32>) -> i32 {
242+
let n = edges.len();
243+
let mut vis = vec![false; n];
244+
let mut ans = -1;
245+
246+
for i in 0..n {
247+
if vis[i] {
248+
continue;
249+
}
250+
let mut j = i as i32;
251+
let mut cycle = Vec::new();
252+
253+
while j != -1 && !vis[j as usize] {
254+
vis[j as usize] = true;
255+
cycle.push(j);
256+
j = edges[j as usize];
257+
}
258+
259+
if j == -1 {
260+
continue;
261+
}
262+
263+
for k in 0..cycle.len() {
264+
if cycle[k] == j {
265+
ans = ans.max((cycle.len() - k) as i32);
266+
break;
267+
}
268+
}
269+
}
270+
ans
271+
}
272+
}
273+
```
274+
237275
<!-- tabs:end -->
238276

239277
<!-- solution:end -->

solution/2300-2399/2360.Longest Cycle in a Graph/README_EN.md

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,23 +202,23 @@ func longestCycle(edges []int) int {
202202
```ts
203203
function longestCycle(edges: number[]): number {
204204
const n = edges.length;
205-
const vis = new Array(n).fill(false);
205+
const vis: boolean[] = Array(n).fill(false);
206206
let ans = -1;
207207
for (let i = 0; i < n; ++i) {
208208
if (vis[i]) {
209209
continue;
210210
}
211211
let j = i;
212212
const cycle: number[] = [];
213-
for (; j != -1 && !vis[j]; j = edges[j]) {
213+
for (; j !== -1 && !vis[j]; j = edges[j]) {
214214
vis[j] = true;
215215
cycle.push(j);
216216
}
217-
if (j == -1) {
217+
if (j === -1) {
218218
continue;
219219
}
220220
for (let k = 0; k < cycle.length; ++k) {
221-
if (cycle[k] == j) {
221+
if (cycle[k] === j) {
222222
ans = Math.max(ans, cycle.length - k);
223223
break;
224224
}
@@ -228,6 +228,44 @@ function longestCycle(edges: number[]): number {
228228
}
229229
```
230230

231+
#### Rust
232+
233+
```rust
234+
impl Solution {
235+
pub fn longest_cycle(edges: Vec<i32>) -> i32 {
236+
let n = edges.len();
237+
let mut vis = vec![false; n];
238+
let mut ans = -1;
239+
240+
for i in 0..n {
241+
if vis[i] {
242+
continue;
243+
}
244+
let mut j = i as i32;
245+
let mut cycle = Vec::new();
246+
247+
while j != -1 && !vis[j as usize] {
248+
vis[j as usize] = true;
249+
cycle.push(j);
250+
j = edges[j as usize];
251+
}
252+
253+
if j == -1 {
254+
continue;
255+
}
256+
257+
for k in 0..cycle.len() {
258+
if cycle[k] == j {
259+
ans = ans.max((cycle.len() - k) as i32);
260+
break;
261+
}
262+
}
263+
}
264+
ans
265+
}
266+
}
267+
```
268+
231269
<!-- tabs:end -->
232270

233271
<!-- solution:end -->
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
impl Solution {
2+
pub fn longest_cycle(edges: Vec<i32>) -> i32 {
3+
let n = edges.len();
4+
let mut vis = vec![false; n];
5+
let mut ans = -1;
6+
7+
for i in 0..n {
8+
if vis[i] {
9+
continue;
10+
}
11+
let mut j = i as i32;
12+
let mut cycle = Vec::new();
13+
14+
while j != -1 && !vis[j as usize] {
15+
vis[j as usize] = true;
16+
cycle.push(j);
17+
j = edges[j as usize];
18+
}
19+
20+
if j == -1 {
21+
continue;
22+
}
23+
24+
for k in 0..cycle.len() {
25+
if cycle[k] == j {
26+
ans = ans.max((cycle.len() - k) as i32);
27+
break;
28+
}
29+
}
30+
}
31+
ans
32+
}
33+
}

solution/2300-2399/2360.Longest Cycle in a Graph/Solution.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
function longestCycle(edges: number[]): number {
22
const n = edges.length;
3-
const vis = new Array(n).fill(false);
3+
const vis: boolean[] = Array(n).fill(false);
44
let ans = -1;
55
for (let i = 0; i < n; ++i) {
66
if (vis[i]) {
77
continue;
88
}
99
let j = i;
1010
const cycle: number[] = [];
11-
for (; j != -1 && !vis[j]; j = edges[j]) {
11+
for (; j !== -1 && !vis[j]; j = edges[j]) {
1212
vis[j] = true;
1313
cycle.push(j);
1414
}
15-
if (j == -1) {
15+
if (j === -1) {
1616
continue;
1717
}
1818
for (let k = 0; k < cycle.length; ++k) {
19-
if (cycle[k] == j) {
19+
if (cycle[k] === j) {
2020
ans = Math.max(ans, cycle.length - k);
2121
break;
2222
}

0 commit comments

Comments
 (0)