GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — leetcode/src/NumberOfIslands.java at master · LeBW/leetcode

Expand file tree

/

NumberOfIslands.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
31 lines (30 loc) · 881 Bytes
/

NumberOfIslands.java

Copy path

File metadata and controls

31 lines (30 loc) · 881 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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.