来源:LeetCode.
请你判断一个 9 x 9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。
数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
注意:
一个有效的数独(部分已被填充)不一定是可解的。
只需要根据以上规则,验证已经填入的数字是否有效即可。
空白格用 ‘.’ 表示。
输入: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 存在, 因此这个数独是无效的。
提示:
接下来看一下解题思路:
有效的数独满足以下三个条件:
可以使用数组,以数字为下标,出现的次数为值,记录 每一行
、每一列
和 每一个小九宫格
中,每个数字出现的次数。只需要遍历数独一次,在遍历的过程中更新数组中的计数,并判断是否满足有效的数独的条件即可。
对于数独的第 i i i 行第 j j j 列的单元格,其中 0 ≤ i , j < 9 0 \le i, j < 9 0≤i,j<9 ,该单元格所在的行下标和列下标分别为 i i i 和 j j j,该单元格所在的小九宫格的行数和列数分别为 ⌊ i 3 ⌋ \Big\lfloor \dfrac{i}{3} \Big\rfloor ⌊3i⌋ 和 ⌊ j 3 ⌋ \Big\lfloor \dfrac{j}{3} \Big\rfloor ⌊3j⌋,其中 0 ≤ ⌊ i 3 ⌋ , ⌊ j 3 ⌋ < 3 0 \le \Big\lfloor \dfrac{i}{3} \Big\rfloor, \Big\lfloor \dfrac{j}{3} \Big\rfloor < 3 0≤⌊3i⌋,⌊3j⌋<3。
具体做法是,创建二维数组 rows \textit{rows} rows 和 columns \textit{columns} columns 分别记录数独的每一行和每一列中的每个数字的出现次数,创建三维数组 subboxes \textit{subboxes} subboxes 记录数独的每一个小九宫格中的每个数字的出现次数,其中 rows [ i ] [ index ] \textit{rows}[i][\textit{index}] rows[i][index]、 columns [ j ] [ index ] \textit{columns}[j][\textit{index}] columns[j][index] 和 subboxes [ ⌊ i 3 ⌋ ] [ ⌊ j 3 ⌋ ] [ index ] \textit{subboxes}\Big[\Big\lfloor \dfrac{i}{3} \Big\rfloor\Big]\Big[\Big\lfloor \dfrac{j}{3} \Big\rfloor\Big]\Big[\textit{index}\Big] subboxes[⌊3i⌋][⌊3j⌋][index] 分别表示数独的第 i i i 行第 j j j 列的单元格所在的行、列和小九宫格中,数字$ \textit{index} + 1$ 出现的次数,其中 0 ≤ index < 9 0 \le \textit{index} < 9 0≤index<9,对应的数字 index + 1 \textit{index} + 1 index+1 满足 1 ≤ index + 1 ≤ 9 1 \le \textit{index} + 1 \le 9 1≤index+1≤9。
如果 board [ i ] [ j ] \textit{board}[i][j] board[i][j] 填入了数字 n n n,则将 rows [ i ] [ n − 1 ] \textit{rows}[i][n - 1] rows[i][n−1]、 columns [ j ] [ n − 1 ] \textit{columns}[j][n - 1] columns[j][n−1] 和 subboxes [ ⌊ i 3 ⌋ ] [ ⌊ j 3 ⌋ ] [ n − 1 ] \textit{subboxes}\Big[\Big\lfloor \dfrac{i}{3} \Big\rfloor\Big]\Big[\Big\lfloor \dfrac{j}{3} \Big\rfloor\Big]\Big[n - 1\Big] subboxes[⌊3i⌋][⌊3j⌋][n−1] 各加 1 1 1。如果更新后的计数大于 1 1 1,则不符合有效的数独的条件,返回 false \text{false} false。
如果遍历结束之后没有出现计数大于 1 1 1 的情况,则符合有效的数独的条件,返回 true \text{true} true。
java实现
class Solution {
public boolean isValidSudoku(char[][] board) {
int row = board.length;
int col = board[0].length;
int[][] rows = new int[row][col];
int[][] cols = new int[row][col];
int[][][] sub = new int[3][3][col];
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
char c = board[i][j];
if (c != '.') {
int index = c - '0' - 1;
rows[i][index] += 1;
cols[index][j] += 1;
sub[i / 3][j / 3][index] += 1;
if (rows[i][index] > 1 || cols[index][j] > 1 || sub[i / 3][j / 3][index] > 1) {
return false;
}
}
}
}
return true;
}
}
C++实现
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
int row = board.size();
int col = board.size();
int rows[row][col];
int cols[row][col];
int subs[3][3][row];
memset(rows,0,sizeof(rows));
memset(cols,0,sizeof(cols));
memset(subs,0,sizeof(subs));
for(int i = 0; i < row; ++i) {
for(int j = 0; j < col; ++j){
char c = board[i][j];
if(c != '.') {
int index = c - '0' - 1;
rows[i][index]++;
cols[index][j]++;
subs[i/3][j/3][index]++;
if(rows[i][index] > 1 || cols[index][j] > 1 || subs[i/3][j/3][index] > 1) {
return false;
}
}
}
}
return true;
}
};
python实现
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
rows = [[0] * 9 for _ in range(9)]
cols = [[0] * 9 for _ in range(9)]
subs = [[[0] * 9 for _ in range(3)] for _ in range(3)]
for i in range(9):
for j in range(9):
c = board[i][j]
if c != '.':
c = int(c) - 1
rows[i][c] += 1
cols[c][j] += 1
subs[int(i/3)][int(j/3)][c] += 1
if rows[i][c] > 1 or cols[c][j] > 1 or subs[int(i/3)][int(j/3)][c] > 1:
return False
return True
时间复杂度: O ( 1 ) O(1) O(1)。数独共有 81 81 81 个单元格,只需要对每个单元格遍历一次即可。
空间复杂度: O ( 1 ) O(1) O(1)。由于数独的大小固定,因此哈希表的空间也是固定的。
用数组来保存每个值出现的位置,需要创建三个数组,分别保存 行、列 和 3 ∗ 3 3*3 3∗3 的小矩阵,需要一定的空间开销。
其实,可以进一步优化,通过位运算,用每一位来记录对应位置值,如果是1说明访问过,如果是0说明没访问过
java实现
class Solution {
public boolean isValidSudoku(char[][] board) {
int row = board.length;
int col = board[0].length;
int[] rows = new int[row];
int[] cols = new int[col];
int[][] subs = new int[3][3];
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
char c = board[i][j];
if (c != '.') {
int mask = 1 << (int)(c - '0');
if((rows[i] & mask) == 0 && (cols[j] & mask) == 0 && (subs[i/3][j/3] & mask) == 0) {
rows[i] += mask;
cols[j] += mask;
subs[i/3][j/3] += mask;
} else {
return false;
}
}
}
}
return true;
}
}
时间复杂度: O ( 1 ) O(1) O(1)。数独共有 81 81 81 个单元格,只需要对每个单元格遍历一次即可。
空间复杂度: O ( 1 ) O(1) O(1)。由于数独的大小固定,因此哈希表的空间也是固定的。