Skip to content

Commit 6df63db

Browse files
committed
Update solution 695 [Java]
1 parent 9236b46 commit 6df63db

File tree

3 files changed

+65
-13
lines changed

3 files changed

+65
-13
lines changed

README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
4141
| 235 | [Lowest Common Ancestor of a Binary Search Tree](https://github.com/doocs/leetcode/tree/master/solution/235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree) | `Tree` |
4242
| 237 | [Delete Node in a Linked List](https://github.com/doocs/leetcode/tree/master/solution/237.Delete%20Node%20in%20a%20Linked%20List) | `Linked List` |
4343
| 344 | [Reverse String](https://github.com/doocs/leetcode/tree/master/solution/344.Reverse%20String) | `Two Pointers`, `String` |
44+
| 695 | [Max Area of Island](https://github.com/doocs/leetcode/tree/master/solution/695.Max%20Area%20of%20Island) | `Array`, `Depth-first Search` |
4445
| 703 | [Kth Largest Element in a Stream](https://github.com/doocs/leetcode/tree/master/solution/703.Kth%20Largest%20Element%20in%20a%20Stream) | `Heap` |
4546
| 707 | [Design Linked List](https://github.com/doocs/leetcode/tree/master/solution/707.Design%20Linked%20List) | `Linked List`, `Design` |
4647
| 876 | [Middle of the Linked List](https://github.com/doocs/leetcode/tree/master/solution/876.Middle%20of%20the%20Linked%20List) | `Linked List` |
@@ -101,7 +102,7 @@ I'm looking for long-term contributors/partners to this repo! Send me [PRs](http
101102
" If you want to go fast, go alone. If you want to go far, go together. And that's the spirit of [teamwork](https://github.com/doocs/leetcode/graphs/contributors) ".
102103

103104
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
104-
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="80px;"/>](https://github.com/yanglbme)<br />[💻](https://github.com/doocs/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="80px;"/>](https://github.com/chakyam)<br />[💻](https://github.com/doocs/leetcode/commits?author=chakyam "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/10081554?v=4" width="80px;"/>](https://github.com/zhkmxx9302013)<br />[💻](https://github.com/doocs/leetcode/commits?author=zhkmxx9302013 "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/40383345?v=4" width="80px;"/>](https://github.com/MarkKuang1991)<br />[💻](https://github.com/doocs/leetcode/commits?author=MarkKuang1991 "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/12371194?v=4" width="80px;"/>](https://github.com/fonxian)<br />[💻](https://github.com/doocs/leetcode/commits?author=fonxian "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/25222367?v=4" width="80px;"/>](https://github.com/zhanary)<br />[💻](https://github.com/doocs/leetcode/commits?author=zhanary "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/42396616?v=4" width="80px;"/>](https://github.com/ZhouTingZhaobiu)<br />[💻](https://github.com/doocs/leetcode/commits?author=ZhouTingZhaobiu "Code") </center> |
105-
|---|---|---|---|---|---|---|
105+
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="80px;"/>](https://github.com/yanglbme)<br />[💻](https://github.com/doocs/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="80px;"/>](https://github.com/chakyam)<br />[💻](https://github.com/doocs/leetcode/commits?author=chakyam "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/10081554?v=4" width="80px;"/>](https://github.com/zhkmxx9302013)<br />[💻](https://github.com/doocs/leetcode/commits?author=zhkmxx9302013 "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/40383345?v=4" width="80px;"/>](https://github.com/MarkKuang1991)<br />[💻](https://github.com/doocs/leetcode/commits?author=MarkKuang1991 "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/12371194?v=4" width="80px;"/>](https://github.com/fonxian)<br />[💻](https://github.com/doocs/leetcode/commits?author=fonxian "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/25222367?v=4" width="80px;"/>](https://github.com/zhanary)<br />[💻](https://github.com/doocs/leetcode/commits?author=zhanary "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/42396616?v=4" width="80px;"/>](https://github.com/ZhouTingZhaobiu)<br />[💻](https://github.com/doocs/leetcode/commits?author=ZhouTingZhaobiu "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/31923541?v=4" width="80px;"/>](https://github.com/zouwx2cs)<br />[💻](https://github.com/doocs/leetcode/commits?author=zouwx2cs "Code") </center> |
106+
|---|---|---|---|---|---|---|---|
106107

107108
<!-- ALL-CONTRIBUTORS-LIST:END -->
+37-11
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
## 岛屿的最大面积
22
### 题目描述
33

4-
给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合。你可以假设二维矩阵的四个边缘都被水包围着。
4+
给定一个包含了一些 0 和 1的非空二维数组 `grid` , 一个 岛屿 是由四个方向 (水平或垂直) 的 `1` (代表土地) 构成的组合。你可以假设二维矩阵的四个边缘都被水包围着。
55

66
找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为0。)
77

8-
示例 1:
8+
**示例 1:**
99
```
1010
[[0,0,1,0,0,0,0,1,0,0,0,0,0],
1111
[0,0,0,0,0,0,0,1,1,1,0,0,0],
@@ -15,21 +15,47 @@
1515
[0,0,0,0,0,0,0,0,0,0,1,0,0],
1616
[0,0,0,0,0,0,0,1,1,1,0,0,0],
1717
[0,0,0,0,0,0,0,1,1,0,0,0,0]]
18-
19-
对于上面这个给定矩阵应返回 6。注意答案不应该是11,因为岛屿只能包含水平或垂直的四个方向的‘1’。
2018
```
2119

22-
示例 2:
20+
对于上面这个给定矩阵应返回 `6`。注意答案不应该是 11,因为岛屿只能包含水平或垂直的四个方向的‘1’。
21+
22+
**示例 2:**
23+
2324
```
2425
[[0,0,0,0,0,0,0,0]]
26+
```
2527

26-
对于上面这个给定的矩阵, 返回 0
28+
对于上面这个给定的矩阵, 返回 `0`
2729

28-
注意: 给定的矩阵grid 的长度和宽度都不超过 50。
29-
```
30+
注意: 给定的矩阵 `grid` 的长度和宽度都不超过 50。
3031

3132
### 解法
32-
搜索
33-
34-
```
33+
利用深度优先搜索,遍历矩阵,搜索每个位置上下左右四个方向,若为 1,则累加 1 并将其置为 0 后继续搜索。最后返回最大值。
34+
35+
```java
36+
class Solution {
37+
public int maxAreaOfIsland(int[][] grid) {
38+
int res = 0;
39+
int m = grid.length;
40+
int n = grid[0].length;
41+
for (int i = 0; i < m; ++i) {
42+
for (int j = 0; j < n; ++j) {
43+
res = Math.max(res, dfs(grid, i, j, m, n));
44+
}
45+
}
46+
return res;
47+
}
48+
49+
private int dfs(int[][] grid, int i, int j, int m, int n) {
50+
if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] == 0) {
51+
return 0;
52+
}
53+
grid[i][j] = 0;
54+
return 1
55+
+ dfs(grid, i - 1, j, m, n)
56+
+ dfs(grid, i + 1, j, m, n)
57+
+ dfs(grid, i, j - 1, m, n)
58+
+ dfs(grid, i, j + 1, m, n);
59+
}
60+
}
3561
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int maxAreaOfIsland(int[][] grid) {
3+
int res = 0;
4+
int m = grid.length;
5+
int n = grid[0].length;
6+
for (int i = 0; i < m; ++i) {
7+
for (int j = 0; j < n; ++j) {
8+
res = Math.max(res, dfs(grid, i, j, m, n));
9+
}
10+
}
11+
return res;
12+
}
13+
14+
private int dfs(int[][] grid, int i, int j, int m, int n) {
15+
if (i < 0 || j < 0 || i >= m || j >= n || grid[i][j] == 0) {
16+
return 0;
17+
}
18+
grid[i][j] = 0;
19+
return 1
20+
+ dfs(grid, i - 1, j, m, n)
21+
+ dfs(grid, i + 1, j, m, n)
22+
+ dfs(grid, i, j - 1, m, n)
23+
+ dfs(grid, i, j + 1, m, n);
24+
}
25+
}

0 commit comments

Comments
 (0)