@@ -0,0 +1,1335 @@
+%0A/**%0A * Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. %0A * An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. %0A * You may assume all four edges of the grid are all surrounded by water.%0A */%0A%0Apublic class Solution %7B%0A  public int numIslands(char%5B%5D%5B%5D grid) %7B%0A    if (grid.length == 0 %7C%7C grid%5B0%5D.length == 0) return 0;%0A    Stack%3Cint%5B%5D%3E stack = new Stack%3Cint%5B%5D%3E();%0A%0A    int count = 0;%0A    for (int i = 0; i %3C grid.length; i ++) %7B%0A      for (int j = 0; j %3C grid%5B0%5D.length; j ++) %7B%0A%0A        // Hit land which can be AT MOST 1 island.%0A        // Therefore increment by only 1 and convert all nearby land into water.%0A        if (grid%5Bi%5D%5Bj%5D == '1') %7B%0A          count++;%0A          stack.clear();%0A          stack.push(new int%5B%5D %7B i, j %7D);%0A          while (!stack.isEmpty()) %7B%0A            int%5B%5D pair = stack.pop();%0A            int x = pair%5B0%5D;%0A            int y = pair%5B1%5D;%0A            if (x %3E= grid.length %7C%7C x %3C 0 %7C%7C y %3E= grid%5B0%5D.length %7C%7C y %3C 0 %7C%7C grid%5Bx%5D%5By%5D == '0') continue;%0A            grid%5Bx%5D%5By%5D = '0';%0A            stack.push(new int%5B%5D %7B x + 1, y %7D);%0A            stack.push(new int%5B%5D %7B x - 1, y %7D);%0A            stack.push(new int%5B%5D %7B x, y + 1 %7D);%0A            stack.push(new int%5B%5D %7B x, y - 1 %7D);%0A%0A          %7D%0A        %7D%0A      %7D%0A    %7D%0A    return count;%0A  %7D%0A%7D

Add number of islands 200