[LeetCode] 36. Valid Sudoku

Determine if a 9 x 9 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 nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.

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.

Example 1:
Example 1
Input: 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”]]
Output: true

Example 2:
Input: 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”]]
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.

Constraints:
board.length == 9
board[i].length == 9
board[i][j] is a digit 1-9 or ‘.’.

有效的数独。

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

数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)

注意:
一个有效的数独(部分已被填充)不一定是可解的。
只需要根据以上规则,验证已经填入的数字是否有效即可。
空白格用 ‘.’ 表示。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/valid-sudoku
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路

这道题的重点是如何判断数字在他所在的行、列和 3x3 宫内只出现一次。这里我们需要三个二维数组,分别记录 9 行,9 列和 9 个 3x3 宫内数字出现的情况。其中行和列的问题比较直观,重点是如何知道某个位置上的数字是属于哪个 3x3 宫。

对于任意一个数字,他在矩阵内的坐标是(i, j),那么他的 cubeIndex = i / 3 * 3 + j / 3

复杂度

时间O(81) - O(1)
空间O(1) - 三个 9x9 的 boolean 数组

代码

Java实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
class Solution {
public boolean isValidSudoku(char[][] board) {
// 数独的行、列和3x3小宫格各有9个
int n = 9;

// 创建3个数组,用于记录行、列和3x3小宫格中出现的数字
boolean[][] rows = new boolean[n][n];
boolean[][] cols = new boolean[n][n];
boolean[][] boxes = new boolean[n][n];

for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (board[i][j] != '.') {
int num = board[i][j] - '1'; // 将字符转换为0-8的数字
int boxIndex = (i / 3) * 3 + j / 3; // 计算小宫格索引

// 如果当前数字在行、列或小宫格中已存在,则返回false
if (rows[i][num] || cols[j][num] || boxes[boxIndex][num]) {
return false;
}

// 记录当前数字在行、列和小宫格中出现过
rows[i][num] = true;
cols[j][num] = true;
boxes[boxIndex][num] = true;
}
}
}

// 如果没有冲突,返回true
return true;
}
}

相关题目

1
2
3
36. Valid Sudoku
37. Sudoku Solver
2133. Check if Every Row and Column Contains All Numbers

[LeetCode] 36. Valid Sudoku
https://shurui91.github.io/posts/1704254951.html
Author
Aaron Liu
Posted on
April 20, 2020
Licensed under