class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
int i,j,m,n;
for(i=0;i<9;i++){
unordered_set<char> s;
for(j=0;j<9;j++){
if(board[i][j]!='.'&&board[i][j]>='1'&&board[i][j]<='9'&&s.find(board[i][j])==s.end()){
s.insert(board[i][j]);
}
else if(board[i][j]!='.'){
return false;
}
}
}
for(j=0;j<9;j++){
unordered_set<char> s;
for(i=0;i<9;i++){
if(board[i][j]!='.'&&board[i][j]>='1'&&board[i][j]<='9'&&s.find(board[i][j])==s.end()){
s.insert(board[i][j]);
}
else if(board[i][j]!='.'){
return false;
}
}
}
for(i=0;i<7;i+=3){
for(j=0;j<7;j+=3){
unordered_set<char> s;
for(m=i;m<i+3;m++){
for(n=j;n<j+3;n++){
if(board[m][n]!='.'&&board[m][n]>='1'&&board[m][n]<='9'&&s.find(board[m][n])==s.end()){
s.insert(board[m][n]);
}
else if(board[m][n]!='.'){
return false;
}
}
}
}
}
return true;
}
};
LeetCode - 36 Valid Sudoku
最新推荐文章于 2022-03-18 15:45:17 发布