-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36.cpp
More file actions
30 lines (25 loc) · 920 Bytes
/
36.cpp
File metadata and controls
30 lines (25 loc) · 920 Bytes
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
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) { //O(9*9)
//bitmasks
vector<int> rows(9);
vector<int> cols(9);
vector<vector<int>> subs(3, vector<int>(3)); //subboxes
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
if(board[i][j] == '.') continue;
int val = board[i][j] - '1';
//check row
if(rows[i] & (1 << val)) return false;
else rows[i] |= (1 << val);
//check col
if(cols[j] & (1 << val)) return false;
else cols[j] |= (1 << val);
//check subbox
if(subs[i/3][j/3] & (1 << val)) return false;
else subs[i/3][j/3] |= (1 << val);
}
}
return true;
}
};