Number of Provinces LeetCode solution in Java
1class Solution {2public int findCircleNum(int[][] isConnected) {3int size = isConnected.length;4boolean[] isCheck = new boolean[size + 1];5int ans = 0;67for (int i = 1; i <= size; i++) {89if (!isCheck[i]) { // Doing BFS if it's false in isCheck[]10Queue<Integer> q = new LinkedList<>();11q.add(i);12ans++; // No. of queue = No. of Graphs1314while (!q.isEmpty()) {15int temp = q.remove();16isCheck[temp] = true;1718for (int j = 0; j < size; j++) {19if (isConnected[temp - 1][j] == 1 && !isCheck[j + 1]) q.add(j + 1);20}21}22}23}24return ans;25}26}