public int NumIslands(char[][] grid) { int rows = grid.Length; if (rows == 0) { return(0); } int cols = grid[0].Length; if (cols == 0) { return(0); } int n = rows * cols; UnionSet uset = new UnionSet(n); for (int row = 0; row < rows; row++) { int index = row * cols; for (int col = 0; col < cols; col++) { if (grid[row][col] == '1') { if (row > 0 && grid[row - 1][col] == '1') { uset.Union(index + col, index - cols + col); } if (col > 0 && grid[row][col - 1] == '1') { uset.Union(index + col, index + col - 1); } } } } HashSet <int> count = new HashSet <int>(); for (int row = 0; row < rows; row++) { int index = row * cols; for (int col = 0; col < cols; col++) { if (grid[row][col] == '1') { count.Add(uset.Find(index + col)); } } } return(count.Count); }
public int FindCircleNum(int[][] isConnected) { if (isConnected.Length == 0 || isConnected == null) { return(0); } UnionSet set = new UnionSet(isConnected.Length); for (int row = 0; row < isConnected.Length; row++) { for (int col = 0; col < isConnected[row].Length; col++) { if (isConnected[row][col] == 1 && row != col) { set.Union(row, col); } } } return(set.GetSets()); }