阅读 30

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

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
复制代码
文章分类
开发工具
文章标签