题目:
确定9x9数独板是否有效。只需要根据以下规则验证填充的单元格:
1)每一行必须包含数字1-9,不能重复;
2)每一列必须包含数字1-9,不能重复;
3)网格的9个3x3子框中的每一个都必须包含数字1-9,不能重复。
【注】数独板(部分填充)可能是有效的,但不一定是可解的。只需要根据上述规则验证填充的单元格。
即检查:
1)每行是否有重复
2)每列是否有重复
3)每个3×3的小格子中是否有重复
Determine if a 9x9 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 9
3x3
sub-boxes of the grid must contain the digits1-9
without repetition.
A partially filled sudoku which is valid. The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
Input:
[
["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
Input:
[
["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.
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.
- The given board contain only digits
1-9
and the character'.'
. - The given board size is always
9x9
.
方法:
分别用三个函数检查三个方面
(时间上有点慢。。估计不太行=-= 可以参考下其他小可爱的~)
代码:
class Solution {
public boolean isValidSudoku(char[][] board) {
return isValidRow(board) && isValidCol(board) && isValidBox(board);
}
// 行是否有重复元素或非法元素
public boolean isValidRow(char[][] board) {
int m = board.length;
int n = board[0].length;
Set<Character> set = new HashSet<Character>();
for(int i=0;i<m;i++) {
int count = 0;
set.removeAll(set);
for(int j=0;j<n;j++) {
char curChar = board[i][j];
if(curChar>'0' && curChar<='9') {
set.add(curChar);
count++;
}
}
if(count!=set.size()) {
return false;
}
}
return true;
}
// 列是否有重复元素或非法元素
public boolean isValidCol(char[][] board) {
int m = board.length;
int n = board[0].length;
Set<Character> set = new HashSet<Character>();
for(int i=0;i<m;i++) {
int count = 0;
set.removeAll(set);
for(int j=0;j<n;j++) {
char curChar = board[j][i];
if(curChar>'0' && curChar<='9') {
set.add(curChar);
count++;
}
}
if(count!=set.size()) {
return false;
}
}
return true;
}
// 3×3的格子里是否有重复
public boolean isValidBox(char[][] board) {
int m = board.length-1;
int n = board[0].length-1;
for(int i=0;i<m;i=i+3) {
for(int j=0;j<n;j=j+3) {
if(checkBox(board, i, j)==false) {
return false;
}
}
}
return true;
}
public boolean checkBox(char[][] board,int m_start, int n_start) {
Set<Character> set = new HashSet<Character>();
int count = 0;
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
char curChar = board[i+m_start][j+n_start];
if(curChar>'0' && curChar<='9') {
set.add(curChar);
count++;
}
}
}
if(count==set.size()) {
return true;
}else {
return false;
}
}
}