Skip to content

Commit 53145ec

Browse files
committed
1
1 parent 2564135 commit 53145ec

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

ValidSudoku/ValidSudoku.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
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));
14+
15+
for (int i = 0; i < 9; i++) {
16+
for (int j = 0; j < 9; j++) {
17+
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])
21+
return false;
22+
visited_cells[cell][x] = true;
23+
visited_row[i][x] = visited_col[j][x] = true;
24+
}
25+
}
26+
}
27+
return true;
28+
}
29+
};

0 commit comments

Comments
 (0)