1. 题目要求
判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。
数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。
2.题目示例
输入:
[
[“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
输入:
[
[“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
3. 题解
不难理解行列子数独分别用HashMap存储
其中子数独桶的索引用:i/3*3 + j/3表示
优化:用boolean数组存储
4. 源代码
public boolean isValidSudoku(char[][] board) {
Map<Character, Integer> row;
Map<Character, Integer> column;
int[][] buk = new int[10][10];
for(int i=0;i<board.length;i++){
row = new HashMap<>();
column = new HashMap<>();
for(int j=0;j<board.length;j++){
// row
if(board[i][j] != '.'){
if(row.containsKey(board[i][j])){
return false;
}else{
row.put(board[i][j], 0);
}
}
// column
if(board[j][i] != '.'){
if(column.containsKey(board[j][i])){
return false;
}else{
column.put(board[j][i], 0);
}
}
int tmpi = i/3;
int tmpj = j/3;
int tmp = tmpi+tmpj;
if(tmp == 1){
if(tmpi>tmpj){
tmp = 3;
}
}else if(tmp == 2){
if(tmpi>tmpj){
tmp = 6;
}else if (tmpi == tmpj){
tmp = 4;
}
}else if(tmp == 3){
if(tmpi > tmpj){
tmp = 7;
}else{
tmp = 5;
}
}else if(tmp == 4){
tmp = 8;
}
if(board[i][j] != '.'){
if(buk[tmp][board[i][j] - '0'] == 1) return false;
buk[tmp][board[i][j] - '0'] = 1;
}
}
}
return true;
}
public boolean isValidSudoku(char[][] board) {
// init data
HashMap<Integer, Integer>[] rows = new HashMap[9];
HashMap<Integer, Integer>[] columns = new HashMap[9];
HashMap<Integer, Integer>[] boxes = new HashMap[9];
for (int i = 0; i < 9; i++) {
rows[i] = new HashMap<Integer, Integer>();
columns[i] = new HashMap<Integer, Integer>();
boxes[i] = new HashMap<Integer, Integer>();
}
// validate a board
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
char num = board[i][j];
if (num != '.') {
int n = (int) num;
int box_index = (i / 3) * 3 + j / 3;
// keep the current cell value
rows[i].put(n, rows[i].getOrDefault(n, 0) + 1);
columns[j].put(n, columns[j].getOrDefault(n, 0) + 1);
boxes[box_index].put(n, boxes[box_index].getOrDefault(n, 0) + 1);
// check if this value has been already seen before
if (rows[i].get(n) > 1 || columns[j].get(n) > 1 || boxes[box_index].get(n) > 1)
return false;
}
}
}
return true;
}
public boolean isValidSudoku(char[][] board) {
boolean[][] rows = new boolean[9][9];
boolean[][] cols = new boolean[9][9];
boolean[][] boxes = new boolean[9][9];
for (int r = 0; r < 9; ++r) {
for (int c = 0; c < 9; ++c) {
if (board[r][c] != '.') {
int value = board[r][c] - '1';
int boxIndex = r / 3 * 3 + c / 3;
if (rows[r][value] || cols[c][value] || boxes[boxIndex][value]) {
return false;
}
rows[r][value] = true;
cols[c][value] = true;
boxes[boxIndex][value] = true;
}
}
}
return true;
}