阅读 187
leetcode每日一题系列-有效的数独-「三层循环暴力解」

leetcode每日一题系列-有效的数独-「三层循环暴力解」

leetcode-36-有效的数独

[博客链接]

菜🐔的学习之路

掘金首页

[题目链接]

题目链接

[github地址]

github地址

[题目描述]

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

数字 1-9 在每一行只能出现一次。 数字 1-9 在每一列只能出现一次。 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图) 数独部分空格内已填入了数字,空白格用 '.' 表示。

注意:

一个有效的数独(部分已被填充)不一定是可解的。 只需要根据以上规则,验证已经填入的数字是否有效即可。  

示例 1:

image.png

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

输入:board = 
[["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 存在, 因此这个数独是无效的。
复制代码

思路一:暴力法

  • 分别扫描行、列、矩阵块
public boolean isValidSudoku(char[][] board) {
    //先列搜
    for (int i = 0; i < board.length; i++) {
        Set<Character> set = new HashSet<>();
        for (int j = 0; j < board.length; j++) {
            if (board[i][j] == '.') continue;
            if (set.contains(board[i][j])) {
                return false;
            }
            set.add(board[i][j]);
        }
    }
    //再行搜
    for (int i = 0; i < board.length; i++) {
        Set<Character> set = new HashSet<>();
        for (int j = 0; j < board.length; j++) {
            if (board[j][i] == '.') continue;
            if (set.contains(board[j][i])) {
                return false;
            }
            set.add(board[j][i]);
        }
    }
    //九宫格搜
    int[][] dirs = new int[][]{{1, 0}, {0, 1}, {0, -1}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
    for (int i = 1; i <= 7; i += 3) {
        for (int j = 1; j <= 7; j += 3) {
            Set<Character> set = new HashSet<>();
            if (board[i][j] != '.') {
                set.add(board[i][j]);
            }
            for (int[] dir : dirs) {
                if (board[i + dir[0]][j + dir[1]] == '.') continue;
                if (set.contains(board[i + dir[0]][j + dir[1]])) {
                    return false;
                }
                set.add(board[i + dir[0]][j + dir[1]]);
            }
        }
    }
    return true;
}
复制代码
  • 时间复杂度O(399) = O(1)
  • 空间复杂度O(1)
文章分类
后端
文章标签