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

Commit7258498

Browse files
refactor 547
1 parentb7c3160 commit7258498

File tree

2 files changed

+36
-38
lines changed

2 files changed

+36
-38
lines changed

‎src/main/java/com/fishercoder/solutions/_547.java

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -37,48 +37,50 @@
3737
*/
3838
publicclass_547 {
3939

40-
publicintfindCircleNum(int[][]M) {
41-
if (M ==null ||M.length ==0 ||M[0].length ==0) {
42-
return0;
43-
}
44-
intm =M.length;//number of rows in this matrix
45-
UnionFindunionFind =newUnionFind(m);
46-
for (inti =0;i <m;i++) {
47-
for (intj =i +1;j <m;j++) {
48-
if (M[i][j] ==1) {
49-
unionFind.union(i,j);
40+
publicstaticclassSolution1 {
41+
publicintfindCircleNum(int[][]M) {
42+
if (M ==null ||M.length ==0 ||M[0].length ==0) {
43+
return0;
44+
}
45+
intm =M.length;//number of rows in this matrix
46+
UnionFindunionFind =newUnionFind(m);
47+
for (inti =0;i <m;i++) {
48+
for (intj =i +1;j <m;j++) {
49+
if (M[i][j] ==1) {
50+
unionFind.union(i,j);
51+
}
5052
}
5153
}
54+
returnunionFind.count;
5255
}
53-
returnunionFind.count;
54-
}
5556

56-
classUnionFind {
57-
intcount;
58-
int[]root;
57+
classUnionFind {
58+
intcount;
59+
int[]root;
5960

60-
publicUnionFind(intm) {
61-
root =newint[m];
62-
for (inti =0;i <m;i++) {
63-
root[i] =i;
61+
publicUnionFind(intm) {
62+
root =newint[m];
63+
for (inti =0;i <m;i++) {
64+
root[i] =i;
65+
}
66+
count =m;
6467
}
65-
count =m;
66-
}
6768

68-
publicvoidunion(inti,intj) {
69-
intx =find(root,i);
70-
inty =find(root,j);
71-
if (x !=y) {
72-
count--;
73-
root[x] =y;//path compression
69+
publicvoidunion(inti,intj) {
70+
intx =find(root,i);
71+
inty =find(root,j);
72+
if (x !=y) {
73+
count--;
74+
root[x] =y;//path compression
75+
}
7476
}
75-
}
7677

77-
publicintfind(int[]ids,inti) {
78-
if (ids[i] ==i) {
79-
returni;
78+
publicintfind(int[]ids,inti) {
79+
if (ids[i] ==i) {
80+
returni;
81+
}
82+
returnfind(ids,ids[i]);
8083
}
81-
returnfind(ids,ids[i]);
8284
}
8385
}
8486

‎src/test/java/com/fishercoder/_547Test.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
packagecom.fishercoder;
22

33
importcom.fishercoder.solutions._547;
4-
importorg.junit.Before;
54
importorg.junit.BeforeClass;
65
importorg.junit.Test;
76

@@ -11,19 +10,16 @@
1110
* Created by fishercoder on 1/9/17.
1211
*/
1312
publicclass_547Test {
14-
privatestatic_547test;
13+
privatestatic_547.Solution1test;
1514
privatestaticintexpected;
1615
privatestaticintactual;
1716
privatestaticint[][]M;
1817

1918
@BeforeClass
2019
publicstaticvoidsetup() {
21-
test =new_547();
20+
test =new_547.Solution1();
2221
}
2322

24-
@Before
25-
publicvoidsetupForEachTest() {}
26-
2723
@Test
2824
publicvoidtest1() {
2925
M =newint[][]{

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp