当前位置:网站首页>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
边栏推荐
- 做 SQL 性能优化真是让人干瞪眼
- 【Rust 笔记】16-输入与输出(下)
- Full Permutation Code (recursive writing)
- R language [import and export of dataset]
- Leetcode-9: palindromes
- Implement an iterative stack
- Some common problems in the assessment of network engineers: WLAN, BGP, switch
- Sqlmap tutorial (1)
- Data visualization chart summary (II)
- [article de jailhouse] jailhouse hypervisor
猜你喜欢

LeetCode 0107. Sequence traversal of binary tree II - another method

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

1.14 - assembly line

7. Processing the input of multidimensional features

Solution to game 10 of the personal field

Leetcode-6108: decrypt messages

Real time clock (RTC)

数据可视化图表总结(一)

Dynamic planning solution ideas and summary (30000 words)

Implement a fixed capacity stack
随机推荐
Error ora-28547 or ora-03135 when Navicat connects to Oracle Database
MIT-6874-Deep Learning in the Life Sciences Week 7
2022 pole technology communication arm virtual hardware accelerates the development of Internet of things software
shared_ Repeated release heap object of PTR hidden danger
Daily question 1984 Minimum difference in student scores
One question per day 2047 Number of valid words in the sentence
Some common problems in the assessment of network engineers: WLAN, BGP, switch
Over fitting and regularization
Implement an iterative stack
Bit mask of bit operation
The difference between CPU core and logical processor
Règlement sur la sécurité des réseaux dans les écoles professionnelles secondaires du concours de compétences des écoles professionnelles de la province de Guizhou en 2022
【Rust 笔记】17-并发(上)
LeetCode 1200. Minimum absolute difference
wordpress切换页面,域名变回了IP地址
Is it impossible for lamda to wake up?
多屏电脑截屏会把多屏连着截下来,而不是只截当前屏
Real time clock (RTC)
MatrixDB v4.5.0 重磅发布,全新推出 MARS2 存储引擎!
API related to TCP connection