leetcode-36-有效的数独

题目描述:

 

 

 方法一:O(1) O(1)

class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        rows = [{} for i in range(9)] 
        columns = [{} for i in range(9)] 
        boxes = [{} for i in range(9)]
        
        for i in range(9):
            for j in range(9):
                num = board[i][j]
                if num!=".":
                    num = int(num)
                    boxes_index = (i//3)*3+j//3
                    rows[i][num] = rows[i].get(num, 0) + 1
                    columns[j][num] = columns[j].get(num,0) + 1
                    boxes[boxes_index][num] = boxes[boxes_index].get(num,0) + 1
                    if rows[i][num]>1 or columns[j][num]>1 or boxes[boxes_index][num]>1:
                        return False
        return True

 

posted @ 2019-07-10 20:29  oldby  阅读(211)  评论(0编辑  收藏  举报