LeetCode习题笔记(36,73)

36

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


73

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的数字,不然会导致结果错误。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值