200. Number of Islands
Posted tobeabetterpig
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了200. Number of Islands相关的知识,希望对你有一定的参考价值。
Given a 2d grid map of ‘1‘s (land) and ‘0‘s (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. Example 1: Input: 11110 11010 11000 00000 Output: 1 Example 2: Input: 11000 11000 00100 00011 Output: 3 If not allowed to change the input : 写一下 Can change 1 to 2 after visited , and change 2 back to 1 after done Or use a Boolean array to check if visited // Dfs mine class Solution { public int numIslands(char[][] grid) { int count = 0; for(int i = 0; i < grid.length; i++){ for(int j = 0; j < grid[0].length; j++){ if(grid[i][j] == ‘1‘){ dfs(i, j, grid); count++; } } } return count; } private void dfs(int row, int col, char[][] grid){ if(row < 0 || col < 0 || row >= grid.length || col >= grid[0].length || grid[row][col] == ‘0‘){ return; }else { grid[row][col] = ‘0‘; dfs(row + 1, col, grid); dfs(row - 1, col, grid); dfs(row, col - 1, grid); dfs(row, col + 1, grid); } } } // Union find , not mine class UF { public int count = 0; public int[] id = null; public UF(int m, int n, char[][] grid) { for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { if(grid[i][j] == ‘1‘) count++; } } id = new int[m * n]; for(int i = 0; i < m * n; i++) { id[i] = i; } } public int find(int p) { while(p != id[p]) { id[p] = id[id[p]]; p = id[p]; } return p; } public boolean isConnected(int p, int q) { int pRoot = find(p); int qRoot = find(q); if(pRoot != qRoot) return false; else return true; } public void union(int p, int q) { int pRoot = find(p); int qRoot = find(q); if(pRoot == qRoot) return; id[pRoot] = qRoot; count--; } } public int numIslands(char[][] grid) { if(grid.length == 0 || grid[0].length == 0) return 0; int m = grid.length, n = grid[0].length; UF uf = new UF(m , n, grid); for(int i = 0; i < m; i++) { for(int j = 0; j < n; j++) { if(grid[i][j] == ‘0‘) continue; int p = i * n + j; int q; if(i > 0 && grid[i - 1][j] == ‘1‘) { // up q = p - n; uf.union(p, q); } if(i < m - 1 && grid[i + 1][j] == ‘1‘) { // down q = p + n; uf.union(p, q); } if(j > 0 && grid[i][j - 1] == ‘1‘) { // left q = p - 1; uf.union(p, q); } if(j < n - 1 && grid[i][j + 1] == ‘1‘) { // right q = p + 1; uf.union(p, q); } } } return uf.count; } // uf my version TLE ?? class Solution { public int numIslands(char[][] grid) { int m = grid.length; int n = grid[0].length; UnionFind uf = new UnionFind(m* n); int count = 0; int[][] directions = {{0, 1}, {0,-1}, {-1, 0}, {1, 0}}; for(int i = 0; i < grid.length; i++){ for(int j = 0; j < grid[0].length; j++){ if(grid[i][j] == ‘1‘){ count++; for (int[] dir : directions){ int x = i + dir[0]; int y = j + dir[1]; if(x < 0 || y < 0 || x >= grid.length || y >= grid[0].length || grid[x][y] == ‘0‘){ continue; }else{ if(uf.union(getKey(i, j, n), getKey(x, y, n))){ count--; } } } } } } return count; } public int getKey(int i, int j, int m){ return i * m + j; } class UnionFind{ int[] id; public UnionFind(int n){ this.id = new int[n]; for(int i = 0; i < n; i++){ id[i] = i; } } public int find (int n){ int root = n; while(n != id[n]){ n = id[n]; } while(n != root){ int next = id[n]; id[n] = root; n = next; } return root; } public boolean union( int q, int p){ int rootp = find(p); int rootq = find(q); if(rootp == rootq){ return false; }else{ id[rootp] = rootq; return true; } } } }
以上是关于200. Number of Islands的主要内容,如果未能解决你的问题,请参考以下文章