Number of Islands Solutions in Java
Number 200
Difficulty Medium
Acceptance 46.9%
Link LeetCode
Solutions
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/number-of-islands/description//// Author : liuyubobobo/// Time : 2017-11-18/// Floodfill - DFS/// Recursion implementation////// Time Complexity: O(n*m)/// Space Complexity: O(n*m)class Solution {private int d[][] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};private int m, n;private boolean visited[][];public int numIslands(char[][] grid) {if(grid == null || grid.length == 0 || grid[0].length == 0)return 0;m = grid.length;n = grid[0].length;visited = new boolean[m][n];int res = 0;for(int i = 0 ; i < m ; i ++)for(int j = 0 ; j < n ; j ++)if(grid[i][j] == '1' && !visited[i][j]){dfs(grid, i, j);res ++;}return res;}private void dfs(char[][] grid, int x, int y){//assert(inArea(x,y));visited[x][y] = true;for(int i = 0; i < 4; i ++){int newx = x + d[i][0];int newy = y + d[i][1];if(inArea(newx, newy) && !visited[newx][newy] && grid[newx][newy] == '1')dfs(grid, newx, newy);}return;}private boolean inArea(int x, int y){return x >= 0 && x < m && y >= 0 && y < n;}public static void main(String[] args) {char grid1[][] = {{'1','1','1','1','0'},{'1','1','0','1','0'},{'1','1','0','0','0'},{'0','0','0','0','0'}};System.out.println((new Solution()).numIslands(grid1));// 1// ---char grid2[][] = {{'1','1','0','0','0'},{'1','1','0','0','0'},{'0','0','1','0','0'},{'0','0','0','1','1'}};System.out.println((new Solution()).numIslands(grid2));// 3}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/number-of-islands/description//// Author : liuyubobobo/// Time : 2018-08-29import java.util.Stack;import javafx.util.Pair;/// Floodfill - DFS/// Non-recursion implementation////// Time Complexity: O(n*m)/// Space Complexity: O(n*m)class Solution2 {private int d[][] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};private int m, n;private boolean visited[][];public int numIslands(char[][] grid) {if(grid == null || grid.length == 0 || grid[0].length == 0)return 0;m = grid.length;n = grid[0].length;visited = new boolean[m][n];int res = 0;for(int i = 0 ; i < m ; i ++)for(int j = 0 ; j < n ; j ++)if(grid[i][j] == '1' && !visited[i][j]){dfs(grid, i, j);res ++;}return res;}private void dfs(char[][] grid, int x, int y){Stack<Pair<Integer, Integer>> q = new Stack<>();q.push(new Pair(x, y));visited[x][y] = true;while(!q.isEmpty()){Pair<Integer, Integer> cur = q.pop();int curx = cur.getKey();int cury = cur.getValue();for(int i = 0; i < 4; i ++){int newX = curx + d[i][0];int newY = cury + d[i][1];if(inArea(newX, newY) && !visited[newX][newY] && grid[newX][newY] == '1'){q.push(new Pair(newX, newY));visited[newX][newY] = true;}}}}private boolean inArea(int x, int y){return x >= 0 && x < m && y >= 0 && y < n;}public static void main(String[] args) {char grid1[][] = {{'1','1','1','1','0'},{'1','1','0','1','0'},{'1','1','0','0','0'},{'0','0','0','0','0'}};System.out.println((new Solution2()).numIslands(grid1));// 1// ---char grid2[][] = {{'1','1','0','0','0'},{'1','1','0','0','0'},{'0','0','1','0','0'},{'0','0','0','1','1'}};System.out.println((new Solution2()).numIslands(grid2));// 3}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/number-of-islands/description//// Author : liuyubobobo/// Time : 2018-08-26import java.util.Queue;import java.util.LinkedList;import javafx.util.Pair;/// Floodfill - BFS/// Time Complexity: O(n*m)/// Space Complexity: O(n*m)class Solution3 {private int d[][] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};private int m, n;private boolean visited[][];public int numIslands(char[][] grid) {if(grid == null || grid.length == 0 || grid[0].length == 0)return 0;m = grid.length;n = grid[0].length;visited = new boolean[m][n];int res = 0;for(int i = 0 ; i < m ; i ++)for(int j = 0 ; j < n ; j ++)if(grid[i][j] == '1' && !visited[i][j]){bfs(grid, i, j);res ++;}return res;}private void bfs(char[][] grid, int x, int y){Queue<Pair<Integer, Integer>> q = new LinkedList<>();q.add(new Pair(x, y));visited[x][y] = true;while(!q.isEmpty()){Pair<Integer, Integer> cur = q.remove();int curx = cur.getKey();int cury = cur.getValue();for(int i = 0; i < 4; i ++){int newX = curx + d[i][0];int newY = cury + d[i][1];if(inArea(newX, newY) && !visited[newX][newY] && grid[newX][newY] == '1'){q.add(new Pair(newX, newY));visited[newX][newY] = true;}}}}private boolean inArea(int x, int y){return x >= 0 && x < m && y >= 0 && y < n;}public static void main(String[] args) {char grid1[][] = {{'1','1','1','1','0'},{'1','1','0','1','0'},{'1','1','0','0','0'},{'0','0','0','0','0'}};System.out.println((new Solution3()).numIslands(grid1));// 1// ---char grid2[][] = {{'1','1','0','0','0'},{'1','1','0','0','0'},{'0','0','1','0','0'},{'0','0','0','1','1'}};System.out.println((new Solution3()).numIslands(grid2));// 3}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/number-of-islands/description//// Author : liuyubobobo/// Time : 2018-08-26import java.util.Arrays;import java.util.HashSet;/// Using Union-Find/// Time Complexity: O(n*m)/// Space Complexity: O(n*m)class Solution4 {private class UnionFind {private int[] rank;private int[] parent;public UnionFind(int count){rank = new int[count];parent = new int[count];for(int i = 0 ; i < count ; i ++){parent[i] = i;rank[i] = 1;}}private int find(int p){while(p != parent[p]){parent[p] = parent[parent[p]];p = parent[p];}return p;}public boolean isConnected(int p , int q){return find(p) == find(q);}public void unionElements(int p, int q){int pRoot = find(p);int qRoot = find(q);if(pRoot == qRoot)return;if(rank[pRoot] < rank[qRoot])parent[pRoot] = qRoot;else if(rank[qRoot] < rank[pRoot])parent[qRoot] = pRoot;else{ // rank[pRoot] == rank[qRoot]parent[pRoot] = qRoot;rank[qRoot] += 1;}}}private int d[][] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};private int m, n;public int numIslands(char[][] grid) {if(grid == null || grid.length == 0 || grid[0].length == 0)return 0;m = grid.length;n = grid[0].length;UnionFind uf = new UnionFind(m * n);for(int i = 0 ; i < m ; i ++)for(int j = 0; j < n ; j ++)if(grid[i][j] == '1')for(int k = 0; k < 4; k ++){int newX = i + d[k][0], newY = j + d[k][1];if(inArea(newX, newY) && grid[newX][newY] == '1')uf.unionElements(i * n + j, newX * n + newY);}HashSet<Integer> islands = new HashSet<>();for(int i = 0 ; i < m ; i ++)for(int j = 0; j < n ; j ++)if(grid[i][j] == '1')islands.add(uf.find(i * n + j));return islands.size();}private boolean inArea(int x, int y){return x >= 0 && x < m && y >= 0 && y < n;}public static void main(String[] args) {char grid1[][] = {{'1','1','1','1','0'},{'1','1','0','1','0'},{'1','1','0','0','0'},{'0','0','0','0','0'}};System.out.println((new Solution4()).numIslands(grid1));// 1// ---char grid2[][] = {{'1','1','0','0','0'},{'1','1','0','0','0'},{'0','0','1','0','0'},{'0','0','0','1','1'}};System.out.println((new Solution4()).numIslands(grid2));// 3}}