8000 update 527 · githubniraj/Leetcode@cac6eaf · GitHub
[go: up one dir, main page]

Skip to content

Commit cac6eaf

Browse files
update 527
1 parent 4440d52 commit cac6eaf

File tree

2 files changed

+32
-18
lines changed

2 files changed

+32
-18
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
public class _547 {
44

55
public static class Solution1 {
6-
public int findCircleNum(int[][] M) {
7-
if (M == null || M.length == 0 || M[0].length == 0) {
6+
public int findCircleNum(int[][] isConnected) {
7+
if (isConnected == null || isConnected.length == 0 || isConnected[0].length == 0) {
88
return 0;
99
}
10-
int m = M.length;//number of rows in this matrix
10+
int m = isConnected.length;//number of rows in this matrix
1111
UnionFind unionFind = new UnionFind(m);
1212
for (int i = 0; i < m; i++) {
1313
for (int j = i + 1; j < m; j++) {
14-
if (M[i][j] == 1) {
14+
if (isConnected[i][j] == 1) {
1515
unionFind.union(i, j);
1616
}
1717
}
1818
}
1919
return unionFind.count;
2020
}
2121

22-
class UnionFind {
22+
static class UnionFind {
2323
int count;
2424
int[] root;
2525

@@ -34,14 +34,16 @@ public UnionFind(int m) {
3434
public void union(int i, int j) {
3535
int x = find(root, i);
3636
int y = find(root, j);
37+
//at this point, x and y should equal, if not, then we should union them into the same value
3738
if (x != y) {
3839
count--;
39-
root[x] = y;//path compression
40+
root[x] = y;//path compression, i.e. union
4041
}
4142
}
4243

4344
public int find(int[] ids, int i) {
4445
if (ids[i] == i) {
46+
//this is the base case, so nothing to recurse on
4547
return i;
8000
4648
}
4749
return find(ids, ids[i]);
Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package com.fishercoder;
22

33
import com.fishercoder.solutions._547;
4-
import org.junit.BeforeClass;
5-
import org.junit.Test;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
66

7-
import static junit.framework.Assert.assertEquals;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
88

99
/**
1010
* Created by fishercoder on 1/9/17.
@@ -13,47 +13,59 @@ public class _547Test {
1313
private static _547.Solution1 test;
1414
private sta 8000 tic int expected;
1515
private static int actual;
16-
private static int[][] M;
16+
private static int[][] isConnected;
1717

18-
@BeforeClass
19-
public static void setup() {
18+
@BeforeEach
19+
public void setup() {
2020
test = new _547.Solution1();
2121
}
2222

2323
@Test
2424
public void test1() {
25-
M = new int[][]{
25+
isConnected = new int[][]{
2626
{1, 1, 0},
2727
{1, 1, 0},
2828
{0, 0, 1},
2929
};
3030
expected = 2;
31-
actual = test.findCircleNum(M);
31+
actual = test.findCircleNum(isConnected);
3232
assertEquals(expected, actual);
3333
}
3434

3535
@Test
3636
public void test2() {
37-
M = new int[][]{
37+
isConnected = new int[][]{
3838
{1, 1, 0},
3939
{1, 1, 1},
4040
{0, 1, 1},
4141
};
4242
expected = 1;
43-
actual = test.findCircleNum(M);
43+
actual = test.findCircleNum(isConnected);
4444
assertEquals(expected, actual);
4545
}
4646

4747
@Test
4848
public void test3() {
49-
M = new int[][]{
49+
isConnected = new int[][]{
5050
{1, 0, 0, 1},
5151
{0, 1, 1, 0},
5252
{0, 1, 1, 1},
5353
{1, 0, 1, 1},
5454
};
5555
expected = 1;
56-
actual = test.findCircleNum(M);
56+
actual = test.findCircleNum(isConnected);
57+
assertEquals(expected, actual);
58+
}
59+
60+
@Test
61+
public void test4() {
62+
isConnected = new int[][]{
63+
{1, 0, 0},
64+
{0, 1, 0},
65+
{0, 0, 1},
66+
};
67+
expected = 3;
68+
actual = test.findCircleNum(isConnected);
5769
assertEquals(expected, actual);
5870
}
5971
}

0 commit comments

Comments
 (0)
0