-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path36. Valid Sudoku.cpp
35 lines (31 loc) · 1.08 KB
/
36. 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
//Runtime: 32 ms, faster than 91.01% of C++ online submissions for Valid Sudoku.
//Memory Usage: 20.2 MB, less than 17.76% of C++ online submissions for Valid Sudoku.
class Solution {
public:
int coord2gridIdx(int i, int j){
/*
grid 0: i: [0-2], j:[0-2]
grid 1: i: [0-2], j:[3-5]
grid 2: i: [0-2], j:[6-8]
*/
return i/3 * 3 + j/3;
};
bool isValidSudoku(vector<vector<char>>& board) {
vector<set<char>> rows(9), cols(9), grids(9);
pair<set<char>::iterator, bool> ret;
for(int i = 0; i < 9; ++i){
for(int j = 0; j < 9; ++j){
char c = board[i][j];
if(c == '.') continue;
int gridIdx = coord2gridIdx(i, j);
ret = rows[i].insert(c);
if(!ret.second) return false;
ret = cols[j].insert(c);
if(!ret.second) return false;
ret = grids[gridIdx].insert(c);
if(!ret.second) return false;
}
}
return true;
}
};