class Solution:
def isValidSudoku(self, board: List[List[str]]) -> bool:
# 1. 判断行中有无相同的数
row, col = len(board), len(board[0])
for i in range(row):
num_set = set()
for j in range(col):
if board[i][j].isnumeric() == False:
continue
elif board[i][j] not in num_set:
num_set.add(board[i][j])
else:
return False
# 2. 判断列中有无相同数
for i in range(col):
num_set = set()
for j in range(row):
if board[j][i].isnumeric() == False:
continue
elif board[j][i] not in num_set:
num_set.add(board[j][i])
else:
return False
# 3. 判断每个粗实线分割的3*3宫内有无相同数字
for i in range(0, row, 3):
for j in range(0, col, 3):
num_set = set()
for sp_x in range(i, i+3):
for sp_y in range(j, j+3):
if board[sp_x][sp_y].isnumeric() == False:
continue
elif board[sp_x][sp_y] not in num_set:
num_set.add(board[sp_x][sp_y])
else:
return False
return True
class Solution:
def setZeroes(self, matrix: List[List[int]]) -> None:
"""
Do not return anything, modify matrix in-place instead.
"""
import copy
matrix_copy = copy.deepcopy(matrix)
for i in range(len(matrix_copy)):
for j in range(len(matrix_copy[i])):
if matrix_copy[i][j] == 0:
for k in range(len(matrix_copy)):
matrix[k][j] = 0
for l in range(len(matrix_copy[i])):
matrix[i][l] = 0
注意不要修改原始matrix的数字,不然会导致结果错误。