GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/java/547_Friend_Circles.java at master · nomodfx/leetcode Expand file tree/ Copy path547_Friend_Circles.javaMore file actions 76 lines (73 loc) · 2.09 KB / Copy path547_Friend_Circles.javaFile metadata and controls76 lines (73 loc) · 2.09 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 public class Solution { public void dfs(int[][] M, int[] visited, int i) { for (int j = 0; j < M.length; j++) { if (M[i][j] == 1 && visited[j] == 0) { visited[j] = 1; dfs(M, visited, j); } } } public int findCircleNum(int[][] M) { // DFS int[] visited = new int[M.length]; int count = 0; for (int i = 0; i < M.length; i++) { if (visited[i] == 0) { dfs(M, visited, i); count++; } } return count; } /*public int findCircleNum(int[][] M) { // BFS int[] visited = new int[M.length]; int count = 0; Queue < Integer > queue = new LinkedList < > (); for (int i = 0; i < M.length; i++) { if (visited[i] == 0) { queue.add(i); while (!queue.isEmpty()) { int s = queue.remove(); visited[s] = 1; for (int j = 0; j < M.length; j++) { if (M[s][j] == 1 && visited[j] == 0) queue.add(j); } } count++; } } return count; }*/ /* // Union find int find(int parent[], int i) { if (parent[i] == -1) return i; return find(parent, parent[i]); } void union(int parent[], int x, int y) { int xset = find(parent, x); int yset = find(parent, y); if (xset != yset) parent[xset] = yset; } public int findCircleNum(int[][] M) { int[] parent = new int[M.length]; Arrays.fill(parent, -1); for (int i = 0; i < M.length; i++) { for (int j = 0; j < M.length; j++) { if (M[i][j] == 1 && i != j) { union(parent, i, j); } } } int count = 0; for (int i = 0; i < parent.length; i++) { if (parent[i] == -1) count++; } return count; }*/ } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |