Skip to content

Commit d56df68

Browse files
authored
Merge pull request codewithdev#44 from ordaboy/master
36. Valid Sudoku Solution in cpp
2 parents 5f4661e + e01cedc commit d56df68

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public:
3+
bool isValidSudoku(vector<vector<char>>& board) {
4+
vector<array<bool,9>> rows(9);
5+
vector<array<bool,9>> cols(9);
6+
vector<array<bool,9>> squares(9);
7+
8+
for (int y = 0; y < 9 ; ++y) {
9+
for (int x = 0; x < 9; ++x) {
10+
if (board[y][x] == '.') continue;
11+
12+
const int current = board[y][x] - '1';
13+
14+
if (rows[y][current]) {
15+
return false;
16+
} else {
17+
rows[y][current] = 1;
18+
}
19+
if (cols[x][current]) {
20+
return false;
21+
} else {
22+
cols[x][current] = 1 ;
23+
}
24+
25+
const int sq_index = (y / 3) * 3 + x / 3;
26+
if (squares[sq_index][current]) {
27+
return false;
28+
} else {
29+
squares[sq_index][current] = 1 ;
30+
}
31+
}
32+
}
33+
return true;
34+
}
35+
};

0 commit comments

Comments
 (0)