LeetCode10 有效的数独

LeetCode10 有效的数独_第1张图片

class Solution(object):
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        for i in range(len(board)):
            for j in range(len(board)):
                if board[i][j] != '.':
                    #判断行重复性
                    for k in range(j+1,9):
                        
                        if board[i][j] == board[i][k]:
                            return False
                    #判断列重复性
                    for h in range(i+1,9):
                        if board[i][j] == board[h][j]:                           
                            return False
                    a = board[i][j]
                    board[i][j] = '0'
                    #判断3*3的宫格内
                    for g in range(i/3*3,i/3*3+3):
                        for q in range(j/3*3,j/3*3+3):
                            if a == board[g][q]:    
                                return False
        return True

参考链接

你可能感兴趣的:(python,leetcode,python)