bool isValidSudoku(vector<vector<char>>& board) {
const int SIZE = 9;
for( int row = 0; row < SIZE; row++ ){
for( int col = 0; col < SIZE; col++ ){
if( board[row][col] != '.' && ( !isValidRowAndCol(board, row, col, board[row][col], SIZE) || !isValidRow(board,row,col,board[row][col], SIZE)
|| !isValidCol(board,row,col,board[row][col], SIZE))){
return false;
}
}
}
return true;
}
bool isValidRow(vector<vector<char>>& board, int row, int col, char ch,const int SIZE){
for(int i=0;i<SIZE;i++)
if( i!= col && ch == board[row][i] )
return false;
return true;
}
bool isValidCol(vector<vector<char>>& board, int row, int col, char ch, const int SIZE){
for(int i=0;i<SIZE;i++)
if( i!=row && ch == board[i][col])
return false;
return true;
}
bool isValidRowAndCol(vector<vector<char>>& board, int row, int col, char ch, const int SIZE){
for(int i = row/3*3; i < row/3*3 + 3; i++)
for(int j = col/3*3; j < col/3*3+3; j++){
if( ch == board[i][j] && (i!=row || j!=col) )
return false;
}
return true;
}
leetcode36
最新推荐文章于 2024-07-25 11:15:54 发布