Skip to content

Commit 7258498

Browse files
refactor 547
1 parent b7c3160 commit 7258498

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
public class _547 {
3939

40-
public int findCircleNum(int[][] M) {
41-
if (M == null || M.length == 0 || M[0].length == 0) {
42-
return 0;
43-
}
44-
int m = M.length;//number of rows in this matrix
45-
UnionFind unionFind = new UnionFind(m);
46-
for (int i = 0; i < m; i++) {
47-
for (int j = i + 1; j < m; j++) {
48-
if (M[i][j] == 1) {
49-
unionFind.union(i, j);
40+
public static class Solution1 {
41+
public int findCircleNum(int[][] M) {
42+
if (M == null || M.length == 0 || M[0].length == 0) {
43+
return 0;
44+
}
45+
int m = M.length;//number of rows in this matrix
46+
UnionFind unionFind = new UnionFind(m);
47+
for (int i = 0; i < m; i++) {
48+
for (int j = i + 1; j < m; j++) {
49+
if (M[i][j] == 1) {
50+
unionFind.union(i, j);
51+
}
5052
}
5153
}
54+
return unionFind.count;
5255
}
53-
return unionFind.count;
54-
}
5556

56-
class UnionFind {
57-
int count;
58-
int[] root;
57+
class UnionFind {
58+
int count;
59+
int[] root;
5960

60-
public UnionFind(int m) {
61-
root = new int[m];
62-
for (int i = 0; i < m; i++) {
63-
root[i] = i;
61+
public UnionFind(int m) {
62+
root = new int[m];
63+
for (int i = 0; i < m; i++) {
64+
root[i] = i;
65+
}
66+
count = m;
6467
}
65-
count = m;
66-
}
6768

68-
public void union(int i, int j) {
69-
int x = find(root, i);
70-
int y = find(root, j);
71-
if (x != y) {
72-
count--;
73-
root[x] = y;//path compression
69+
public void union(int i, int j) {
70+
int x = find(root, i);
71+
int y = find(root, j);
72+
if (x != y) {
73+
count--;
74+
root[x] = y;//path compression
75+
}
7476
}
75-
}
7677

77-
public int find(int[] ids, int i) {
78-
if (ids[i] == i) {
79-
return i;
78+
public int find(int[] ids, int i) {
79+
if (ids[i] == i) {
80+
return i;
81+
}
82+
return find(ids, ids[i]);
8083
}
81-
return find(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
package com.fishercoder;
22

33
import com.fishercoder.solutions._547;
4-
import org.junit.Before;
54
import org.junit.BeforeClass;
65
import org.junit.Test;
76

@@ -11,19 +10,16 @@
1110
* Created by fishercoder on 1/9/17.
1211
*/
1312
public class _547Test {
14-
private static _547 test;
13+
private static _547.Solution1 test;
1514
private static int expected;
1615
private static int actual;
1716
private static int[][] M;
1817

1918
@BeforeClass
2019
public static void setup() {
21-
test = new _547();
20+
test = new _547.Solution1();
2221
}
2322

24-
@Before
25-
public void setupForEachTest() {}
26-
2723
@Test
2824
public void test1() {
2925
M = new int[][]{

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy