1
1
class Solution {
2
2
public:
3
- bool visited_cells[20 ][20 ];
4
- bool visited_row[20 ][20 ];
5
- bool visited_col[20 ][20 ];
6
-
7
- bool isValidSudoku (vector<vector<char >>& board) {
8
- // Start typing your C/C++ solution below
9
- // DO NOT write int main() function
10
-
11
- memset (visited_cells, false , sizeof (visited_cells));
12
- memset (visited_row, false , sizeof (visited_row));
13
- memset (visited_col, false , sizeof (visited_col));
3
+ bool isValidSudoku (vector<vector<char >> &board) {
4
+ vector<vector<bool >> rows (9 , vector<bool >(9 , false ));
5
+ vector<vector<bool >> cols (9 , vector<bool >(9 , false ));
6
+ vector<vector<bool >> cells (9 , vector<bool >(9 , false ));
14
7
15
8
for (int i = 0 ; i < 9 ; i++) {
16
9
for (int j = 0 ; j < 9 ; j++) {
17
10
if (board[i][j] != ' .' ) {
18
- int cell = (i / 3 ) * 3 + j / 3 ;
19
- int x = board[i][j] - ' 0' ;
20
- if (visited_cells[cell][x] || visited_row[i][x] || visited_col[j][x])
11
+ int x = board[i][j] - ' 1' ;
12
+ if (rows[i][x] || cols[j][x] || cells[(j/3 )*3 +i/3 ][x]) {
21
13
return false ;
22
- visited_cells[cell][x] = true ;
23
- visited_row[i][x] = visited_col[j][x] = true ;
14
+ }
15
+ rows[i][x] = true ;
16
+ cols[j][x] = true ;
17
+ cells[(j/3 )*3 +i/3 ][x] = true ;
24
18
}
25
19
}
26
20
}
27
21
return true ;
28
22
}
29
- };
23
+ };
0 commit comments