-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36. Valid Sudoku.cpp
More file actions
28 lines (28 loc) · 927 Bytes
/
36. Valid Sudoku.cpp
File metadata and controls
28 lines (28 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
for(int i=0;i<9;i++){
map<char,bool>row;
map<char,bool>colum;
map<char,bool>subgrid;
for(int j=0;j<9;j++){
if(board[i][j]!='.'){
if(row[board[i][j]]==true)return false;
row[board[i][j]]=true;
}//row check
if(board[j][i]!='.'){
if(colum[board[j][i]]==true)return false;
colum[board[j][i]]=true;
}//colum check
if(board[i/3*3+j/3][i%3*3+j%3]!='.'){
if(subgrid[board[i/3*3+j/3][i%3*3+j%3]]==true)return false;
subgrid[board[i/3*3+j/3][i%3*3+j%3]]=true;
}//subgrid check
}
}
return true;
}
};
/*
主要思想是行、列、3X3格子都测一遍,前两个比较简单,唯一难点是后面那个需要推导
*/