Skip to content

Commit 516a05c

Browse files
authored
Merge pull request doocs#115 from Mrtj2016/dev
0200.Number of Islands-python
2 parents d680e09 + ab14f0c commit 516a05c

File tree

4 files changed

+117
-2
lines changed

4 files changed

+117
-2
lines changed
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
## 岛屿的个数
2+
### 题目描述
3+
给定一个由`'1'`(陆地)和`'0'`(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。
4+
```
5+
示例 1:
6+
输入:
7+
11110
8+
11010
9+
11000
10+
00000
11+
输出: 1
12+
```
13+
```
14+
示例 2:
15+
输入:
16+
11000
17+
11000
18+
00100
19+
00011
20+
输出: 3
21+
```
22+
### 解法
23+
对网格进行遍历,每遇到一个岛屿,都将其中陆地`1`全部变成`0`,则遇到岛屿的总数即为所求。
24+
```python
25+
class Solution:
26+
def numIslands(self, grid):
27+
def dp(x, y):
28+
if x >= 0 and x < len(grid) and y >= 0 and y < len(grid[x]) and grid[x][y] == '1':
29+
grid[x][y] = '0'
30+
dp(x-1, y)
31+
dp(x+1, y)
32+
dp(x, y-1)
33+
dp(x, y+1)
34+
ans = 0
35+
for i in range(len(grid)):
36+
for j in range(len(grid[i])):
37+
if grid[i][j] == '1':
38+
ans += 1
39+
dp(i, j)
40+
return ans
41+
42+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def numIslands(self, grid):
3+
"""
4+
:type grid: List[List[str]]
5+
:rtype: int
6+
"""
7+
def dp(x, y):
8+
if x >= 0 and x < len(grid) and y >= 0 and y < len(grid[x]) and grid[x][y] == '1':
9+
grid[x][y] = '0'
10+
dp(x-1, y)
11+
dp(x+1, y)
12+
dp(x, y-1)
13+
dp(x, y+1)
14+
ans = 0
15+
for i in range(len(grid)):
16+
for j in range(len(grid[i])):
17+
if grid[i][j] == '1':
18+
ans += 1
19+
dp(i, j)
20+
return ans
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
## 阻碍逃脱者
2+
### 题目描述
3+
4+
你在进行一个简化版的吃豆人游戏。你从`(0, 0)`点开始出发,你的目的地是` (target[0], target[1])`。地图上有一些阻碍者,第 `i` 个阻碍者从 `(ghosts[i][0], ghosts[i][1])`出发。
5+
6+
每一回合,你和阻碍者们可以同时向东,西,南,北四个方向移动,每次可以移动到距离原位置`1`个单位的新位置。
7+
8+
如果你可以在任何阻碍者抓住你之前到达目的地(阻碍者可以采取任意行动方式),则被视为逃脱成功。如果你和阻碍者同时到达了一个位置(包括目的地)都不算是逃脱成功。
9+
10+
当且仅当你有可能成功逃脱时,输出 `True`
11+
12+
```
13+
示例 1:
14+
输入:
15+
ghosts = [[1, 0], [0, 3]]
16+
target = [0, 1]
17+
输出:true
18+
解释:
19+
你可以直接一步到达目的地(0,1),在(1, 0)或者(0, 3)位置的阻碍者都不可能抓住你。
20+
```
21+
```
22+
示例 2:
23+
输入:
24+
ghosts = [[1, 0]]
25+
target = [2, 0]
26+
输出:false
27+
解释:
28+
你需要走到位于(2, 0)的目的地,但是在(1, 0)的阻碍者位于你和目的地之间。
29+
```
30+
```
31+
示例 3:
32+
输入:
33+
ghosts = [[2, 0]]
34+
target = [1, 0]
35+
输出:false
36+
解释:
37+
阻碍者可以和你同时达到目的地。
38+
```
39+
40+
41+
### 解法
42+
若你会被阻碍者抓住,则此阻碍者必定不会晚于你到达终点,因此我们只需要考虑阻碍者是否会比你晚到达终点即可,若存在不比你晚的,则返回`false`,其他返回`true`
43+
44+
```python
45+
class Solution:
46+
def escapeGhosts(self, ghosts, target):
47+
flag=abs(target[0])+abs(target[1])
48+
for i in ghosts:
49+
if abs(i[0]-target[0])+abs(i[1]-target[1])<=flag:
50+
return False
51+
else:
52+
return True
53+
```

solution/0789.Escape The Ghosts/Solution.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ def escapeGhosts(self, ghosts, target):
55
:type target: List[int]
66
:rtype: bool
77
"""
8-
flag=abs(target[0])+abs(target[1])
8+
flag = abs(target[0]) + abs(target[1])
99
for i in ghosts:
10-
if abs(i[0]-target[0])+abs(i[1]-target[1])<=flag:
10+
if abs(i[0] - target[0]) + abs(i[1] - target[1]) <= flag:
1111
return False
1212
else:
1313
return True

0 commit comments

Comments
 (0)