Number of Provinces LeetCode solution in Java

1
class Solution {
2
public int findCircleNum(int[][] isConnected) {
3
int size = isConnected.length;
4
boolean[] isCheck = new boolean[size + 1];
5
int ans = 0;
6

7
for (int i = 1; i <= size; i++) {
8

9
if (!isCheck[i]) { // Doing BFS if it's false in isCheck[]
10
Queue<Integer> q = new LinkedList<>();
11
q.add(i);
12
ans++; // No. of queue = No. of Graphs
13

14
while (!q.isEmpty()) {
15
int temp = q.remove();
16
isCheck[temp] = true;
17

18
for (int j = 0; j < size; j++) {
19
if (isConnected[temp - 1][j] == 1 && !isCheck[j + 1]) q.add(j + 1);
20
}
21
}
22
}
23
}
24
return ans;
25
}
26
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0