题目

https://leetcode.com/problems/valid-sudoku/
[Leetcode with python] 36. Valid Sudoku (# Hash table)
[Leetcode with python] 36. Valid Sudoku (# Hash table)

解题

遍历三次矩阵,三个规则都检验一次。

class Solution(object):
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        for row in board:
            d = {}
            for i in row:
                if i in d:
                    return False
                elif i != '.':
                    d[i] = 1
        for j in range(9):
            d = {}
            for i in range(9):
                if board[i][j] in d:
                    return False
                elif board[i][j] != '.':
                    d[board[i][j]] = 1
        for i in range(3):
            for j in range(3):
                d = {}
                for m in range(3*i, 3*(i+1)):
                    for n in range(3*j, 3*(j+1)):
                        if board[m][n] in d:
                            return False
                        elif board[m][n] != '.':
                            d[board[m][n]] = 1 
        return True

相关文章:

  • 2021-10-22
  • 2021-11-19
  • 2022-02-13
  • 2022-02-21
  • 2021-12-15
猜你喜欢
  • 2022-01-09
  • 2022-01-23
  • 2021-06-09
  • 2021-07-01
  • 2021-11-20
  • 2021-10-18
相关资源
相似解决方案