请你判断一个 9 x 9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。
数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
注意:
一个有效的数独(部分已被填充)不一定是可解的。
只需要根据以上规则,验证已经填入的数字是否有效即可。
空白格用 '.' 表示。
示例 1:
输入: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 存在, 因此这个数独是无效的。
提示:
board.length == 9
board[i].length == 9
board[i][j] 是一位数字(1-9)或者 '.'
解题思路
有效数独的要求是就是:
1、每一行1-9都要出现一遍,不能缺失、不能重复
2、每一列1-9都要出现一遍,不能缺失、不能重复
3、每一个 3x3 的小宫格内1-9都要出现一遍,不能缺失、不能重复
所以对每个不为空的数字都检查一遍是否在同一列、同一行、同一宫格内已存在相同的数字。存在即为无效数独。不存在则加入该行、该列、该宫内。
全部检查完毕则为有效数独
java实现
class Solution {
public boolean isValidSudoku(char[][] board) {
HashSet[] rows = new HashSet[9];
HashSet[] cols = new HashSet[9];
HashSet[] blocks = new HashSet[9];
for(int i=0; i<9; i++){
rows[i] = new HashSet();
cols[i] = new HashSet();
blocks[i] = new HashSet();
}
for(int i=0; i<9; i++){
for(int j=0; j<9; j++){
int blockid = convertRolCol(i, j);
if(board[i][j]!='.'){
if(rows[i].contains(board[i][j])){
return false;
}
if(cols[j].contains(board[i][j])){
return false;
}
if(blocks[blockid].contains(board[i][j])){
return false;
}
rows[i].add(board[i][j]);
cols[j].add(board[i][j]);
blocks[blockid].add(board[i][j]);
}
}
}
return true;
}
public int convertRolCol(int i, int j){
if(i>=0&&i<3){
if(j>=0&&j<3){
return 0;
}else if(j>=3&&j<6){
return 1;
}else {
return 2;
}
}else if(i>=3&&i<6){
if(j>=0&&j<3){
return 3;
}else if(j>=3&&j<6){
return 4;
}else {
return 5;
}
}else {
if(j>=0&&j<3){
return 6;
}else if(j>=3&&j<6){
return 7;
}else {
return 8;
}
}
}
}
python实现
class Solution(object):
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
rows = [set() for _ in range(9)]
cols = [set() for _ in range(9)]
blocks = [set() for _ in range(9)]
for i in range(9):
for j in range(9):
blockId = self.convertRolCol(i, j)
if board[i][j] != '.':
if board[i][j] in rows[i]:
return False
if board[i][j] in cols[j]:
return False
if board[i][j] in blocks[blockId]:
return False
rows[i].add(board[i][j])
cols[j].add(board[i][j])
blocks[blockId].add(board[i][j])
return True
def convertRolCol(self, i, j):
if i in [0, 1, 2]:
if j in [0, 1, 2]:
return 0
elif j in [3, 4, 5]:
return 1
else:
return 2
elif i in [3, 4, 5]:
if j in [0, 1, 2]:
return 3
elif j in [3, 4, 5]:
return 4
else:
return 5
else:
if j in [0, 1, 2]:
return 6
elif j in [3, 4, 5]:
return 7
else:
return 8