LeetCode
36 Valid Sudoku
题目
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.
解答
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
const int cnt = 9;
bool row[cnt][cnt] = {false};
bool col[cnt][cnt] = {false};
bool block[cnt][cnt] = {false};
for(int i = 0;i < cnt;i++)
{
for(int j = 0;j < cnt;j++)
{
if(board[i][j] != '.')
{
int num = board[i][j] - '0' - 1;
if(row[num][i] == true)
return false;
row[num][i] = true;
if(col[num][j] == true)
return false;
col[num][j] = true;
int b = i/3*3 + j/3;
if(block[num][b] == true)
return false;
block[num][b] = true;
}
}
}
return true;
}
};