leetcode:有效的數獨

xiaoxinlong發表於2024-10-19

題目:有效的數獨

link: https://leetcode.cn/problems/valid-sudoku/description/?envType=study-plan-v2&envId=top-interview-150

    def isValidSudoku(self, board: List[List[str]]) -> bool:

        rows = [[0 for i in range(9)] for j in range(9)]
        coloumns = [[0 for i in range(9)] for j in range(9)]

        sub = [[[0 for _ in range(9)] for i in range(3)] for j in range(3)]

        for i in range(9):
            for j in range(9):
                ss = board[i][j]
                if ss!=".":
                    index = int(ss) - 1
                    rows[i][index] += 1
                    coloumns[j][index] += 1
                    # print(i//3,j//3,index)
                    sub[i//3][j//3][index] += 1
                    if(rows[i][index]>1 or coloumns[j][index]>1 or sub[i//3][j//3][index]>1):
                        # print(rows,coloumns,sub)
                        return False
        return True
        ```

相關文章