Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf0798d1

Browse files
body of graph valid tree
1 parentf1cf5cf commitf0798d1

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

‎MEDIUM/src/medium/GraphValidTree.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
11
packagemedium;
22

3+
importjava.util.Arrays;
4+
35
/**
46
* Created by fishercoder1534 on 9/29/16.
57
*/
68
publicclassGraphValidTree {
9+
10+
publicbooleanvalidTree(intn,int[][]edges){
11+
int[]nums =newint[n];
12+
Arrays.fill(nums, -1);
13+
14+
for(inti =0;i <edges.length;i++){
15+
intx =find(nums,edges[i][0]);
16+
inty =find(nums,edges[i][1]);
17+
18+
if(x ==y)returnfalse;
19+
20+
//union
21+
nums[x] =y;
22+
}
23+
24+
returnedges.length ==n-1;
25+
}
26+
27+
intfind(int[]nums,inti){
28+
if(nums[i] == -1)returni;
29+
returnfind(nums,nums[i]);
30+
}
731
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp