当前位置:网站首页>One question per day 1020 Number of enclaves
One question per day 1020 Number of enclaves
2022-07-05 06:12:00 【A big pigeon】
topic :
Give you a size of m x n The binary matrix of grid , among 0 Represents an ocean cell 、1 Represents a land cell .
once Move It refers to walking from one land cell to another ( On 、 Next 、 Left 、 Right ) Land cells or across grid The boundary of the .
Return to grid unable Land that leaves the grid boundary in any number of movements ( enclave ) The number of cells .
Explain : use 2 Mark the land you can leave .
First mark the outermost land 2. And then from 2 set out , Depth first search the surrounding land is marked 2.
Last grid The unmarked land in is the enclave .
class Solution:
def numEnclaves(self, grid: List[List[int]]) -> int:
def dfs(i,j):
nonlocal grid,m,n
if i<0 or j<0 or i>=m or j >=n :
return
if grid[i][j]==1:
grid[i][j] = 2 # Departable land
dfs(i+1,j)
dfs(i-1,j)
dfs(i,j-1)
dfs(i,j+1)
return
m, n = len(grid), len(grid[0])
cnt = 0
for i in range(m):
grid[i][0] = 2 if grid[i][0] == 1 else 0
grid[i][n-1] = 2 if grid[i][n-1] == 1 else 0
for j in range(n):
grid[0][j] = 2 if grid[0][j] == 1 else 0
grid[m-1][j] = 2 if grid[m-1][j] == 1 else 0
for i in range(m):
for j in range(n):
if grid[i][j] == 2:
dfs(i+1,j)
dfs(i-1,j)
dfs(i,j-1)
dfs(i,j+1)
for i in range(m):
for j in range(n):
if grid[i][j] == 1:
cnt += 1
return cnt
边栏推荐
- 1039 Course List for Student
- Leetcode-22: bracket generation
- CPU内核和逻辑处理器的区别
- QQ电脑版取消转义符输入表情
- 1996. number of weak characters in the game
- Leetcode-6111: spiral matrix IV
- 1041 Be Unique
- QQ computer version cancels escape character input expression
- 7. Processing the input of multidimensional features
- QT判断界面当前点击的按钮和当前鼠标坐标
猜你喜欢

1.14 - assembly line

Arduino 控制的 RGB LED 无限镜

7. Processing the input of multidimensional features

Appium基础 — 使用Appium的第一个Demo

Brief introduction to tcp/ip protocol stack

4. 对象映射 - Mapping.Mapster

Doing SQL performance optimization is really eye-catching

Real time clock (RTC)

MIT-6874-Deep Learning in the Life Sciences Week 7

Leetcode-6110: number of incremental paths in the grid graph
随机推荐
数据可视化图表总结(一)
SPI details
Bit mask of bit operation
884. Uncommon words in two sentences
Brief introduction to tcp/ip protocol stack
[rust notes] 16 input and output (Part 1)
【Rust 笔记】15-字符串与文本(下)
QQ computer version cancels escape character input expression
Daily question 2013 Detect square
leetcode-3:无重复字符的最长子串
Implement an iterative stack
Common optimization methods
API related to TCP connection
Navicat连接Oracle数据库报错ORA-28547或ORA-03135
Smart construction site "hydropower energy consumption online monitoring system"
On the characteristics of technology entrepreneurs from Dijkstra's Turing Award speech
SPI 详解
LeetCode 0107.二叉树的层序遍历II - 另一种方法
1040 Longest Symmetric String
LeetCode 1200. Minimum absolute difference