-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValid_Sudoku.cpp
More file actions
38 lines (35 loc) · 1.1 KB
/
Valid_Sudoku.cpp
File metadata and controls
38 lines (35 loc) · 1.1 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
#include "unordered_set";
#include "vector";
using namespace std;
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
vector<unordered_set<char>> cms(9);
vector<unordered_set<char>> rows(9);
vector<unordered_set<char>> blks(9);
for (int i=0; i<9; i++) {
for (int j=0; j<9; j++) {
char c = board[i][j];
if (c != '.') {
int blkIdx = (i/3)*3+j/3;
if (cms[j].find(c)!=cms[j].end()) {
return false;
} else {
cms[j].insert(c);
}
if (rows[i].find(c)!=rows[j].end()) {
return false;
} else {
rows[i].insert(c);
}
if (blks[blkIdx].find(c)!=blks[blkIdx].end()) {
return false;
} else {
blks[blkIdx].insert(c);
}
}
}
}
return true;
}
};