一道很基本功的问题,耗费了自己大量的时间找bug,原来bug就是,看下面
public class Solution {
public boolean isValidSudoku(char[][] board) {
if (board == null || board.length == 0 || board[0] == null || board[0].length == 0) {
return true;
}
for (int r = 0; r < board.length; r++) {
Set<Character> set = new HashSet<>();
for (int c = 0; c < board[0].length; c++) {
char t = board[r][c];
if (t == '.') {
continue;
}
if (set.contains(t)) {
return false;
} else {
set.add(t);
}
}
}
for (int c = 0; c < board[0].length; c++) {
Set<Character> set = new HashSet<>();
for (int r = 0; r < board.length; r++) {
char t = board[r][c];
if (t == '.') {
continue;
}
if (set.contains(t)) {
return false;
} else {
set.add(t);
}
}
}
//
//int x = 0, y = 0;
//
int x = 0;
for (; x < board.length; x = x + 3) {
//for (; y < board[0].length; y = y + 3) {
for (int y = 0; y < board[0].length; y = y + 3) {
Set<Character> set = new HashSet<>();
for (int i = x; i < x + 3; i++) {
for (int j = y; j < y + 3; j++) {
char t = board[i][j];
if (t == '.') {
continue;
}
if (set.contains(t)) {
return false;
} else {
set.add(t);
}
}
}
}
}
return true;
}
}