-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36.cpp
More file actions
54 lines (51 loc) · 1.68 KB
/
36.cpp
File metadata and controls
54 lines (51 loc) · 1.68 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
vector<bool> exists = vector<bool>(10); // numbers 1-9 can fit!
// rows = 9
for (int i=0; i<9; i++) {
for (int j=0; j<9; j++) {
if (board[i][j] != '.') {
if (exists[board[i][j] - '0']) {
return false;
}
exists[board[i][j] - '0'] = true;
}
}
exists.clear();
exists.resize(10, false);
}
// columns
for (int j=0; j<9; j++) {
for (int i=0; i<9; i++) {
if (board[i][j] != '.') {
if (exists[board[i][j] - '0']) {
return false;
}
exists[board[i][j] - '0'] = true;
}
}
exists.clear();
exists.resize(10, false);
}
// squares
for(int x = 0; x < 3; x++) { // which square # x-pos
for (int y = 0; y < 3; y++) { // which square # y-pos
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
char curTile = board[i+(x * 3)][j + (y * 3)];
if (curTile != '.') {
if (exists[curTile - '0']) {
return false;
}
exists[curTile - '0'] = true;
}
}
}
exists.clear();
exists.resize(10, false);
}
}
return true;
}
};