# leetcode 有效的数独

504 / 504 个通过测试用例

import java.util.HashSet;
import java.util.Set;

class Solution {
public boolean isValidSudoku(char[][] board) {
StringBuilder str = new StringBuilder();
Set<Character> set = new HashSet<>();
int split = 0;//记录 . 负号的个数
//检测行是否有重复
for (int j = 0; j < 9; j++) {

for (int i = 0; i < 9; i++) {
if (board[j][i] == '.') {
split++;
}
if (set.size() < 9 - split){
System.out.println("行重复了");
return false;
}
split = 0;
set.clear();
}
//检测列有重复
for (int i = 0; i <9 ; i++) {

for (int j = 0; j < 9; j++) {
if (board[j][i] == '.') {
split++;
}
if (set.size() < 9 - split){
System.out.println("列重复了");
return false;
}
split = 0;
set.clear();
}
//检测九宫格
int ni = 3;
int nj = 3;
int temp = 0;
int tempi = 0;
set.clear();
split = 0;
for (int i = 0; i < 3; i++) {
for (; tempi <ni ; tempi++) {
for (int j = 0+temp; j < nj+temp; j++) {
if (board[tempi][j] == '.')
split++;
else
if ((j+1) % 3 == 0 && (tempi+1) %3 ==0) {
if (set.size() < 9 - split){
System.out.println("九宫格重复");
return false;
}
split = 0;
set.clear();
break;
}
}
if ((tempi+1) == 3){
if (temp < 6) {
temp += 3;
tempi = -1;
}
}
if ((tempi+1) == 6){
if (temp < 6) {
temp += 3;
tempi = 2;
}
}
if ((tempi+1) == 9){
if (temp < 6) {
temp += 3;
tempi = 5;
}
}
}
ni += 3;
temp = 0;
}

return true;
}
}
03-21 4982

10-23 1246
11-30 4413
05-04 640
04-25 312
09-03 347
12-04 79
02-05 94
06-21 420
07-30 691
10-02 1262
05-17 518
09-27 160
02-02 76
12-15 279