Re: [閒聊] 每日leetcode

作者: DJYOMIYAHINA (通通打死)   2024-08-10 00:57:34
亂七八糟
跟我的人生依樣
一二三四五
def numMagicSquaresInside(self, grid: List[List[int]]) -> int:
m, n = len(grid), len(grid[0])
ans = 0
for i in range(m):
for j in range(n):
if i+2<m and j+2<n:
cnt = set()
for ii in range(i,i+3):
for jj in range(j,j+3):
if grid[ii][jj] <= 9:
cnt.add(grid[ii][jj])
if len(cnt) != 9:
continue
sums = []
# row
for k in range(3):
sums.append(grid[i+k][j]+grid[i+k][j+1]+grid[i+k][j+2])
# col
for k in range(3):
sums.append(grid[i][j+k]+grid[i+1][j+k]+grid[i+2][j+k])
# diagonal
sums.append(grid[i][j]+grid[i+1][j+1]+grid[i+2][j+2])
sums.append(grid[i][j+2]+grid[i+1][j+1]+grid[i+2][j])
if all([s==15 for s in sums]):
ans += 1
return ans
作者: rainkaras (rainkaras)   2024-08-10 00:59:00
寶 你最棒
作者: sixB (6B)   2024-08-10 01:07:00
你是最棒的

Links booklink

Contact Us: admin [ a t ] ucptt.com