forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0036-valid-sudoku.cpp
38 lines (31 loc) · 1.05 KB
/
0036-valid-sudoku.cpp
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
/*
Determine if a 9x9 Sudoku board is valid (no repeats)
Boolean matrices to store seen values. Check rows, cols, 3x3 sub-boxes
Time: O(cnt^2)
Space: O(cnt^2)
*/
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
const int cnt = 9;
bool row[cnt][cnt] = {false};
bool col[cnt][cnt] = {false};
bool sub[cnt][cnt] = {false};
for(int r = 0; r < cnt; ++r){
for(int c = 0; c < cnt; ++c){
if(board[r][c] == '.')
continue; // if not number pass
int idx = board[r][c] - '0' - 1; //char to num idx
int area = (r/3) * 3 + (c/3);
//if number already exists
if(row[r][idx] || col[c][idx] || sub[area][idx]){
return false;
}
row[r][idx] = true;
col[c][idx] = true;
sub[area][idx] = true;
}
}
return true;
}
};