diff --git a/solution/0200.Number of Islands/README.md b/solution/0200.Number of Islands/README.md new file mode 100644 index 0000000000000..a900baf9239fe --- /dev/null +++ b/solution/0200.Number of Islands/README.md @@ -0,0 +1,42 @@ +## 岛屿的个数 +### 题目描述 +给定一个由`'1'`(陆地)和`'0'`(水)组成的的二维网格,计算岛屿的数量。一个岛被水包围,并且它是通过水平方向或垂直方向上相邻的陆地连接而成的。你可以假设网格的四个边均被水包围。 +``` +示例 1: +输入: +11110 +11010 +11000 +00000 +输出: 1 +``` +``` +示例 2: +输入: +11000 +11000 +00100 +00011 +输出: 3 +``` +### 解法 +对网格进行遍历,每遇到一个岛屿,都将其中陆地`1`全部变成`0`,则遇到岛屿的总数即为所求。 +```python +class Solution: + def numIslands(self, grid): + def dp(x, y): + if x >= 0 and x < len(grid) and y >= 0 and y < len(grid[x]) and grid[x][y] == '1': + grid[x][y] = '0' + dp(x-1, y) + dp(x+1, y) + dp(x, y-1) + dp(x, y+1) + ans = 0 + for i in range(len(grid)): + for j in range(len(grid[i])): + if grid[i][j] == '1': + ans += 1 + dp(i, j) + return ans + +``` diff --git a/solution/0200.Number of Islands/Solution.py b/solution/0200.Number of Islands/Solution.py new file mode 100644 index 0000000000000..c50f3d4a3c649 --- /dev/null +++ b/solution/0200.Number of Islands/Solution.py @@ -0,0 +1,20 @@ +class Solution: + def numIslands(self, grid): + """ + :type grid: List[List[str]] + :rtype: int + """ + def dp(x, y): + if x >= 0 and x < len(grid) and y >= 0 and y < len(grid[x]) and grid[x][y] == '1': + grid[x][y] = '0' + dp(x-1, y) + dp(x+1, y) + dp(x, y-1) + dp(x, y+1) + ans = 0 + for i in range(len(grid)): + for j in range(len(grid[i])): + if grid[i][j] == '1': + ans += 1 + dp(i, j) + return ans diff --git a/solution/0789.Escape The Ghosts/README.md b/solution/0789.Escape The Ghosts/README.md index e69de29bb2d1d..f3dc9dda3feed 100644 --- a/solution/0789.Escape The Ghosts/README.md +++ b/solution/0789.Escape The Ghosts/README.md @@ -0,0 +1,53 @@ +## 阻碍逃脱者 +### 题目描述 + +你在进行一个简化版的吃豆人游戏。你从`(0, 0)`点开始出发,你的目的地是` (target[0], target[1])`。地图上有一些阻碍者,第 `i` 个阻碍者从 `(ghosts[i][0], ghosts[i][1])`出发。 + +每一回合,你和阻碍者们可以同时向东,西,南,北四个方向移动,每次可以移动到距离原位置`1`个单位的新位置。 + +如果你可以在任何阻碍者抓住你之前到达目的地(阻碍者可以采取任意行动方式),则被视为逃脱成功。如果你和阻碍者同时到达了一个位置(包括目的地)都不算是逃脱成功。 + +当且仅当你有可能成功逃脱时,输出 `True`。 + +``` +示例 1: +输入: +ghosts = [[1, 0], [0, 3]] +target = [0, 1] +输出:true +解释: +你可以直接一步到达目的地(0,1),在(1, 0)或者(0, 3)位置的阻碍者都不可能抓住你。 +``` +``` +示例 2: +输入: +ghosts = [[1, 0]] +target = [2, 0] +输出:false +解释: +你需要走到位于(2, 0)的目的地,但是在(1, 0)的阻碍者位于你和目的地之间。 +``` +``` +示例 3: +输入: +ghosts = [[2, 0]] +target = [1, 0] +输出:false +解释: +阻碍者可以和你同时达到目的地。 +``` + + +### 解法 +若你会被阻碍者抓住,则此阻碍者必定不会晚于你到达终点,因此我们只需要考虑阻碍者是否会比你晚到达终点即可,若存在不比你晚的,则返回`false`,其他返回`true`。 + +```python +class Solution: + def escapeGhosts(self, ghosts, target): + flag=abs(target[0])+abs(target[1]) + for i in ghosts: + if abs(i[0]-target[0])+abs(i[1]-target[1])<=flag: + return False + else: + return True +``` diff --git a/solution/0789.Escape The Ghosts/Solution.py b/solution/0789.Escape The Ghosts/Solution.py index 6de85946fc94e..044452bf15770 100644 --- a/solution/0789.Escape The Ghosts/Solution.py +++ b/solution/0789.Escape The Ghosts/Solution.py @@ -5,9 +5,9 @@ def escapeGhosts(self, ghosts, target): :type target: List[int] :rtype: bool """ - flag=abs(target[0])+abs(target[1]) + flag = abs(target[0]) + abs(target[1]) for i in ghosts: - if abs(i[0]-target[0])+abs(i[1]-target[1])<=flag: + if abs(i[0] - target[0]) + abs(i[1] - target[1]) <= flag: return False else: return True