https://leetcode.com/problems/valid-sudoku/
判断是否为valid的数独问题
如何计算一个九宫格还是基本功啊!!!
如何通过一个双层循环就判断row+col+matrix三个约束条件
public class Solution {
public boolean isValidSudoku(char[][] board) {
for (int i = 0; i < 9; i++) {
HashSet<Character> row = new HashSet<>();
HashSet<Character> col = new HashSet<>();
HashSet<Character> matrix = new HashSet<>();
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.' && !row.add(board[i][j])) {
return false;
}
if (board[j][i] != '.' && !col.add(board[j][i])) {
return false;
}
int rowIndex = 3 * (i / 3);
int colIndex = 3 * (i % 3);
if (board[rowIndex + j / 3][colIndex + j % 3] != '.' && !matrix.add(board[rowIndex + j / 3][colIndex + j % 3])) {
return false;
}
}
}
return true;
}
}