算法题--判断数独已填入数据的合法性

image.png

0. 链接

1. 需求

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

Each row must contain the digits 1-9 without repetition.
Each column must contain the digits 1-9 without repetition.
Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

A partially filled sudoku which is valid.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

Example 1:

Input:
[
  ["5","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
Output: true

Example 2:

Input:
[
  ["8","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being 
    modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.

Note:

A Sudoku board (partially filled) could be valid but is not necessarily solvable.
Only the filled cells need to be validated according to the mentioned rules.
The given board contain only digits 1-9 and the character '.'.
The given board size is always 9x9.

2. 思路: 保存每行、每列、每个9宫格的出现元素集合, 然后依次判断81个格子的数据, 时间复杂度和空间复杂度均为O(m*n)

2.1 算法步骤

1. 初始化每行、每列、每个九宫格的出现元素集合line_dicts, column_dicts, grid_dicts
2. 循环处理每个格子元素, 
如对于第i行第j列元素nums[i][j], 如果不是缺省值,
则
    1) 判断line_dicts[i]是否包含nums[i][j], 包含则返回False
    2) 判断column_dicts[j]是否包含nums[i][j], 包含则返回False
    3) 判断grid_dicts[i // 3][j // 3]是否包含nums[i][j], 包含则返回False
将nums[i][j]添加到line_dicts[i], column_dicts[j], grid_dicts[i // 3][j // 3]
3. 返回True

2.2 代码

# coding:utf8


class Solution:
    def isValidSudoku(self, board):
        line_dicts = [set() for i in range(9)]
        columns_dicts = [set() for i in range(9)]
        grid_dicts = [[set() for i in range(3)] for j in range(3)]

        for i in range(9):
            for j in range(9):
                element = board[i][j]
                if element == '.':
                    continue

                grid_line, grid_column = i // 3, j // 3

                if element in line_dicts[i] \
                        or element in columns_dicts[j] \
                        or element in grid_dicts[grid_line][grid_column]:
                    return False
                line_dicts[i].add(element)
                columns_dicts[j].add(element)
                grid_dicts[grid_line][grid_column].add(element)

        return True


solution = Solution()
print(solution.isValidSudoku([
  ["5","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
])) # True
print(solution.isValidSudoku([
  ["8","3",".",".","7",".",".",".","."],
  ["6",".",".","1","9","5",".",".","."],
  [".","9","8",".",".",".",".","6","."],
  ["8",".",".",".","6",".",".",".","3"],
  ["4",".",".","8",".","3",".",".","1"],
  ["7",".",".",".","2",".",".",".","6"],
  [".","6",".",".",".",".","2","8","."],
  [".",".",".","4","1","9",".",".","5"],
  [".",".",".",".","8",".",".","7","9"]
])) # False

2.3 结果

image.png

你可能感兴趣的:(算法题--判断数独已填入数据的合法性)