36. Valid Sudoku

61 篇文章 0 订阅
54 篇文章 0 订阅

在这里插入图片描述
1 check each row
2 check each cols
3 check each box

class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        return self.rowsValid(board) and self.colsValid(board) and self.boxesValid(board)
    box_size = 3
    board_size = 9
    def rowsValid(self, board):
        # check each row
        for r in board:
            seen = set()
            for item in r:
                if item in seen:
                    return False
                elif item != '.':
                    seen.add(item)
        return True
    
    def colsValid(self, board):
        # check each column
        for i in range(self.board_size):
            seen = set()
            for r in board:
                if r[i] in seen:
                    return False
                elif r[i] != '.':
                    seen.add(r[i])
        return True
    
    def boxesValid(self, board):
        # check each box
        for boxx in range(0, self.board_size, self.box_size):
            for boxy in range(0, self.board_size, self.box_size):
                seen = set()
                for x in range(self.box_size):
                    row = board[boxx + x]
                    for y in range(self.box_size):
                        item = row[boxy + y]
                        if item in seen:
                            return False
                        elif item != '.':
                            seen.add(item)
        return True
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值