Skip to content

Commit

Permalink
feat: $200 增加Java Code
Browse files Browse the repository at this point in the history
  • Loading branch information
azl397985856 committed Apr 20, 2020
1 parent 007e524 commit 9a6e5ed
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion problems/200.number-of-islands.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,37 @@ Output: 3

## 代码

* 语言支持:JS, python3
* 语言支持:JS, python3,Java

Java Code:

```java
public int numIslands(char[][] grid) {
if (grid == null || grid.length == 0 || grid[0].length == 0) return 0;

int count = 0;
for (int row = 0; row < grid.length; row++) {
for (int col = 0; col < grid[0].length; col++) {
if (grid[row][col] == '1') {
dfs(grid, row, col);
count++;
}
}
}
return count;
}

private void dfs(char[][] grid,int row,int col) {
if (row<0||row== grid.length||col<0||col==grid[0].length||grid[row][col]!='1') {
return;
}
grid[row][col] = '0';
dfs(grid, row-1, col);
dfs(grid, row+1, col);
dfs(grid, row, col+1);
dfs(grid, row, col-1);
}
```

Javascript Code:
```js
Expand Down

0 comments on commit 9a6e5ed

Please sign in to comment.