36. 有效的数独 - 力扣(LeetCode) (leetcode-cn.com)
class Solution {
public boolean isValidSudoku(char[][] board) {
int n = board.length;
Set<Character> set1 = new HashSet<>();
Set<Character> set2 = new HashSet<>();
Set<Character> set3 = new HashSet<>();
for(int i=0;i<n;i++){
set1.clear();
set2.clear();
set3.clear();
for(int j=0;j<n;j++){
if(board[i][j]!='.'&&!set1.add(board[i][j]))
return false;
if(board[j][i]!='.'&&!set2.add(board[j][i]))
return false;
int x = (i / 3) * 3 + j / 3;
int y = (i % 3) * 3 + j % 3;
if (board[x][y] != '.' && !set3.add(board[x][y])) {
return false;
}
}
}
return true;
}
}