Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits
1-9
without repetition. - Each column must contain the digits
1-9
without repetition. - Each of the 9
3x3
sub-boxes of the grid must contain the digits1-9
without repetition. A partially filled sudoku which is valid.
The Sudoku board could be partially filled, where empty cells are filled with the character
'.'
.class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board)
{
const int num=9;
bool row[num][num]={false};
bool col[num][num]={false};
bool smallArea[num][num]={false};
for(int i=0;i<board.size();i++)
{
for(int j=0;j<board.size();j++)
{
if(board[i][j]=='.')
continue;
int index=board[i][j]-'1';
if(row[i][index]==true)
{
return false;
}
row[i][index]=true;
if(col[j][index]==true)
{
return false;
}
col[j][index]=true;
int area=(i/3)*3+j/3;
if(smallArea[area][index]==true)
{
return false;
}
smallArea[area][index]=true;
}
}
return true;
}
};