zoukankan      html  css  js  c++  java
  • 132-36. 有效的数独

    判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。(第一个我写的,暴力匹配,脑子不够直接暴力来.)
    class Solution(object):
        def isValidSudoku2(self, board):
            """
            :type board: List[List[str]]
            :rtype: bool
            """
            point_list = [[1, 1], [1, 4], [1, 7],
                          [4, 1], [4, 4], [4, 7],
                          [7, 1], [7, 4], [7, 7]]
    
            for i in range(9):
                # 处理横向
                for j in range(1, 10):
                    if board[i].count(str(j)) > 1:
                        return False
    
                column_list = []
                for j in range(9):
                    column_list.append(board[j][i])
                print(column_list.count(str(5)))
                for k in range(1, 10):
                    if column_list.count(str(k)) > 1:
                        return False
    
                point = point_list[i]
                column_list2 = []
                column_list2.append(board[point[0]][point[1]])
                column_list2.append(board[point[0]-1][point[1]])
                column_list2.append(board[point[0]+1][point[1]])
                column_list2.append(board[point[0]][point[1]-1])
                column_list2.append(board[point[0]][point[1]+1])
                column_list2.append(board[point[0]+1][point[1]-1])
                column_list2.append(board[point[0]-1][point[1]+1])
                column_list2.append(board[point[0]+1][point[1]+1])
                column_list2.append(board[point[0]-1][point[1]-1])
                for m in range(1, 10):
                    if column_list2.count(str(m)) > 1:
                        return False
            return True
    
        def isValidSudoku(self, board):
            hash_row = [{} for i in range(9)]
            hash_coloum = [{} for i in range(9)]
            hash_box = [{} for i in range(9)]
            for i in range(9):
                for j in range(9):
                    if board[i][j] == '.':
                        pass
                    elif board[i][j] in hash_row[i]:
                        return False
                    elif board[i][j] in hash_coloum[j]:
                        return False
                    elif board[i][j] in hash_box[int(j / 3 + (i / 3 * 3))]:
                        return False
                    else:
                        hash_row[i][board[i][j]] = 1
                        hash_coloum[j][board[i][j]] = 1
                        hash_box[int(j / 3 + (i / 3 * 3))][board[i][j]] = 1
            return True
    
    
    if __name__ == '__main__':
        s1 = Solution()
        board = [[".",".","4",".",".",".","6","3","."],
                 [".",".",".",".",".",".",".",".","."],
                 ["5",".",".",".",".",".",".","9","."],
                 [".",".",".","5","6",".",".",".","."],
                 ["4",".","3",".",".",".",".",".","1"],
                 [".",".",".","7",".",".",".",".","."],
                 [".",".",".","5",".",".",".",".","."],
                 [".",".",".",".",".",".",".",".","."],
                 [".",".",".",".",".",".",".",".","."]]
    
        print(s1.isValidSudoku(board))
    
  • 相关阅读:
    vue
    vue
    vue 中使用style(样式)
    vue 中使用class(样式)
    第17课
    第16课
    第15课
    第14课
    第13课
    第12课
  • 原文地址:https://www.cnblogs.com/liuzhanghao/p/14246279.html
Copyright © 2011-2022 走看看