力扣每日一题2021-09-17有效的数独

文章目录


36.有效的数独

题目描述

有效的数独


思路:遍历

力扣每日一题2021-09-17有效的数独

class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        for i in range(9):
            nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
            for j in range(9):
                if board[i][j] == '.':
                    continue
                else:
                    if board[i][j] in nums:
                        nums.remove(board[i][j])
                    else:
                        return False
        
        for i in range(9):
            nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
            for j in range(9):
                if board[j][i] == '.':
                    continue
                else:
                    if board[j][i] in nums:
                        nums.remove(board[j][i])
                    else:
                        return False
        
        for i in range(0, 9, 3):
            for j in range(0, 9, 3):
                nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
                for k in range(0, 3):
                    for l in range(0, 3):
                        if board[i+k][j+l] == '.':
                            continue
                        else:
                            if board[i+k][j+l] in nums:
                                nums.remove(board[i+k][j+l])
                            else:
                                return False
        
        return True
上一篇:C语言 3x3格五子棋对战电脑


下一篇:leetcode36.有效数独——leetcode每日一题2021.9.17