Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6d33b32

Browse files
update 200.number-of-islands.java
1 parent 212f6e8 commit 6d33b32

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

‎200.number-of-islands.java‎

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* @lc app=leetcode id=200 lang=java
3+
*
4+
* [200] Number of Islands
5+
*
6+
* https://leetcode.com/problems/number-of-islands/description/
7+
*
8+
* algorithms
9+
* Medium (48.72%)
10+
* Total Accepted: 956.8K
11+
* Total Submissions: 2M
12+
* Testcase Example: '[["1","1","1","1","0"],["1","1","0","1","0"],["1","1","0","0","0"],["0","0","0","0","0"]]'
13+
*
14+
* Given an m x n 2d grid map of '1's (land) and '0's (water), return the
15+
* number of islands.
16+
*
17+
* An island is surrounded by water and is formed by connecting adjacent lands
18+
* horizontally or vertically. You may assume all four edges of the grid are
19+
* all surrounded by water.
20+
*
21+
*
22+
* Example 1:
23+
*
24+
*
25+
* Input: grid = [
26+
* ⁠ ["1","1","1","1","0"],
27+
* ⁠ ["1","1","0","1","0"],
28+
* ⁠ ["1","1","0","0","0"],
29+
* ⁠ ["0","0","0","0","0"]
30+
* ]
31+
* Output: 1
32+
*
33+
*
34+
* Example 2:
35+
*
36+
*
37+
* Input: grid = [
38+
* ⁠ ["1","1","0","0","0"],
39+
* ⁠ ["1","1","0","0","0"],
40+
* ⁠ ["0","0","1","0","0"],
41+
* ⁠ ["0","0","0","1","1"]
42+
* ]
43+
* Output: 3
44+
*
45+
*
46+
*
47+
* Constraints:
48+
*
49+
*
50+
* m == grid.length
51+
* n == grid[i].length
52+
* 1 <= m, n <= 300
53+
* grid[i][j] is '0' or '1'.
54+
*
55+
*
56+
*/
57+
58+
59+
// public class Solution {
60+
//
61+
// private int n;
62+
// private int m;
63+
//
64+
// public int numIslands(char[][] grid) {
65+
// int count = 0;
66+
// n = grid.length;
67+
// if (n == 0) return 0;
68+
// m = grid[0].length;
69+
// for (int i = 0; i < n; i++){
70+
// for (int j = 0; j < m; j++) {
71+
// if (grid[i][j] == '1') {
72+
// DFSMarking(grid, i, j);
73+
// ++count;
74+
// }
75+
// }
76+
// }
77+
// return count;
78+
// }
79+
//
80+
// private void DFSMarking(char[][] grid, int i, int j) {
81+
// if (i < 0 || j < 0 || i >= n || j >= m || grid[i][j] != '1') return;
82+
// grid[i][j] = '0';
83+
// DFSMarking(grid, i + 1, j);
84+
// DFSMarking(grid, i - 1, j);
85+
// DFSMarking(grid, i, j + 1);
86+
// DFSMarking(grid, i, j - 1);
87+
// }
88+
// }
89+
90+
class Solution {
91+
92+
private int W;
93+
private int H;
94+
95+
public int numIslands(char[][] grid) {
96+
97+
W = grid.length;
98+
H = grid[0].length;
99+
100+
if (W == 0) return 0;
101+
102+
int islands = 0;
103+
for (int i = 0; i < W; ++i) {
104+
for (int j = 0; j < H; ++j) {
105+
if (grid[i][j] == '1') {
106+
dfs(grid, i, j);
107+
++islands;
108+
}
109+
}
110+
}
111+
return islands;
112+
}
113+
114+
public void dfs(char[][] grid, int x, int y) {
115+
if (isOutOfBounds(grid, x, y) || grid[x][y] != '1') return;
116+
grid[x][y] = '0';
117+
dfs(grid, x, y + 1); // top
118+
dfs(grid, x + 1, y); // right
119+
dfs(grid, x, y - 1); // down
120+
dfs(grid, x - 1, y); // left
121+
}
122+
123+
public boolean isOutOfBounds(char[][] grid, int x, int y) {
124+
if (x < 0 || y < 0 || x >= W || y >= H)
125+
return true;
126+
127+
return false;
128+
}
129+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /