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.
class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
set<int> nums;
for(int i = 0; i < 9; ++i)
{
nums.clear();
for(int j = 0; j < 9; ++j)
{
if('.' != board[i][j])
{
if(nums.count(board[i][j]) > 0)
return false;
else
nums.insert(board[i][j]);
}
}
}
for(int i = 0; i < 9; ++i)
{
nums.clear();
for(int j = 0; j < 9; ++j)
{
if('.' != board[j][i])
{
if(nums.count(board[j][i]) > 0)
return false;
else
nums.insert(board[j][i]);
}
}
}
for(int i = 0; i < 3; ++i)
{
for(int j = 0; j < 3; ++j)
{
nums.clear();
for(int x = 0; x < 3; ++x)
{
for(int y = 0; y < 3; ++y)
{
if('.' != board[i*3+x][j*3+y])
{
if(nums.count(board[i*3+x][j*3+y]) > 0)
return false;
else
nums.insert(board[i*3+x][j*3+y]);
}
}
}
}
}
return true;
}
};