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/src/NumberOfIslands.java at master · LeBW/leetcode Expand file tree/ Copy pathNumberOfIslands.javaMore file actions 31 lines (30 loc) · 881 Bytes / Copy pathNumberOfIslands.javaFile metadata and controls31 lines (30 loc) · 881 Bytes 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 /** * 200. Number of Islands * 深度优先遍历 * @author LBW */ public class NumberOfIslands { public int numIslands(char[][] grid) { int m = grid.length, n = grid[0].length; int res = 0; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { if (grid[i][j] == '1') { res += 1; // traverse the whole land and change 1 to 0 traverse(grid, i, j); } } } return res; } private void traverse(char[][] grid, int i, int j) { if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == '0') return; grid[i][j] = '0'; traverse(grid, i - 1, j); traverse(grid, i + 1, j); traverse(grid, i, j - 1); traverse(grid, i, j + 1); } } |
| You are visitor 01402775 — 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. |