11class Solution {
22public:
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 ));
147
158 for (int i = 0 ; i < 9 ; i++) {
169 for (int j = 0 ; j < 9 ; j++) {
1710 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]) {
2113 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 ;
2418 }
2519 }
2620 }
2721 return true ;
2822 }
29- };
23+ };
0 commit comments