❤leetcode,python2❤判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_22795513/article/details/80636525
class Solution(object):
    def isValidSudoku(self, board):
        """
        :type board: List[List[str]]
        :rtype: bool
        """
        for i in xrange(9):
            hang = board[i]
            lie = []
            for j in range(9):
                lie.append(board[j][i])
            for m in hang:
                if hang.count(m)>1 and m!='.':
                    return False
            for n in lie:
                if lie.count(n)>1 and n!='.':
                    return False
            for j in xrange(9):
                if i%3==0 and j%3==0:
                    small = []
                    for p in range(i,i+3):
                        for q in range(j,j+3):
                            if board[p][q] != '.':
                                small.append(board[p][q])
                    if len(small) != len(list(set(small))):
                        print i,j,p,q
                        return False
        return True

猜你喜欢

转载自blog.csdn.net/qq_22795513/article/details/80636525
今日推荐