题目
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 ‘.’.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
思路
数独有效的意思是只要一行,一列,一个九宫格里没有重复数字即可。
代码
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
int x[9][9] = {0};
int y[9][9] = {0};
int z[9][9] = {0};
for (int i = 0; i < board.size(); i++) {
for (int j = 0; j < board.size(); j++) {
if (board[i][j] != '.') {
int index = board[i][j] - '0' - 1;
if (x[i][index] == 1 || y[j][index] == 1 || z[i/3*3 + j/3][index] == 1) {
return false;
} else {
x[i][index] = 1;
y[j][index] = 1;
z[i/3*3 + j/3][index] = 1;
}
}
}
}
return true;
}
};