Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
创建行、列、格三个布尔数组,依次判断数字是否已经被使用过了
public class Solution {
public boolean isValidSudoku(char[][] board) {
{
boolean [][]used1 = new boolean[9][9];
boolean [][]used2 = new boolean[9][9];
boolean [][]used3 = new boolean[9][9];
for(int i = 0; i < board.length; ++ i)
for(int j = 0; j < board[i].length; ++ j)
if(board[i][j] != '.')
{
int num = board[i][j] - '0' - 1, k = i / 3 * 3 + j / 3;
if(used1[i][num] || used2[j][num] || used3[k][num])
return false;
used1[i][num] = used2[j][num] = used3[k][num] = true;
}
return true;
}
}
}