[LeetCode] 36. 有效的数独

题目链接: https://leetcode-cn.com/problems/valid-sudoku/

题目描述:

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

  1. 数字 1-9 在每一行只能出现一次。
  2. 数字 1-9 在每一列只能出现一次。
  3. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。

示例:

示例 1:

输入:
[
  ["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

示例 2:

输入:
[
  ["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
解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。
     但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。

说明:

  • 一个有效的数独(部分已被填充)不一定是可解的。
  • 只需要根据以上规则,验证已经填入的数字是否有效即可。
  • 给定数独序列只包含数字 1-9 和字符 '.'
  • 给定数独永远是 9x9 形式的。

思路:

首先还是理清题意,就是每一行,每一列,每一个小正方形都不能重复出现一个字母,如下图所示:

所以我们最直接想到就是,就是记录它的行,列,和小正方形的值,有重复就false

思路一:

我们用一个字典,分别记录行,列,和小正方形!

行,列我们直接可以用数字表示,小正方形如何表示呢?

这里,我们发现一个规律,我们可以把小正方形变成用二维数组唯一标识,比如(0,0)表示左上角那个,(1,1)表示中间那个,他们和行列的关系就是(i//3,j//3),

所以任何位置我们都能找出它在哪个行,哪个列,哪个正方形里!

时间复杂度都是常数级的

思路二:

上面我们用的空间复杂度有点多,要想办法改进空间复杂度,

我们有点小技巧,我们只需要用一个集合就可以搞定!

比如我们把board[i][j]

用字符串:

表示行:(i) + board[i][j]

表示列: board[i][j] + (j)

表示小正方形:(i) + board[i][j] + (j)

就直接可以用一个集合搞定!


关注我的知乎专栏,了解更多的解题技巧,大家共同进步!

代码:

思路一

class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        from collections import defaultdict
        row = defaultdict(set)
        col = defaultdict(set)
        small_square = defaultdict(set)
        
        for i in range(9):
            for j in range(9):
                if board[i][j].isdigit():
                    if board[i][j] not in row[i] \
                    and board[i][j] not in col[j] \
                    and board[i][j] not in small_square[(i // 3,j // 3)]:
                        row[i].add(board[i][j]) 
                        col[j].add(board[i][j])
                        small_square[(i // 3, j // 3)].add(board[i][j])
                    else:
                        return False
        return True

思路二

python

class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        one_set = set()
        
        for i in range(9):
            for j in range(9):
                if board[i][j].isdigit():
                    row = "(" + str(i) + ")" + board[i][j]
                    col = board[i][j] + "(" + str(j) + ")"
                    small_square = "(" + str(i//3)+ ")" + board[i][j] +  "(" + str(j//3) + ")"
                    if row in one_set or col in one_set or small_square in one_set:
                        return False
                    one_set.update([row,col,small_square])
        return True

java

class Solution {
    public boolean isValidSudoku(char[][] board) {
        Set<String> one_set = new HashSet<>();
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] != '.') {
                    String row = "(" + i + ")" + board[i][j];
                    String col = board[i][j] + "(" + j + ")";
                    String small_square = "(" + i / 3 + ")" + board[i][j] + "(" + j / 3 + ")";
                    if (!one_set.add(row) || !one_set.add(col) || !one_set.add(small_square)) return false;
                }
            }
        }
        return true;
        
    }
}

posted on 2019-05-09 17:14  威行天下  阅读(249)  评论(0编辑  收藏  举报

导航