-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36.validSudoku.cpp
More file actions
57 lines (49 loc) · 1.76 KB
/
36.validSudoku.cpp
File metadata and controls
57 lines (49 loc) · 1.76 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
55
56
57
/*
Determine if a 9 x 9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
Each row must contain the digits 1-9 without repetition.
Each column must contain the digits 1-9 without repetition.
Each of the nine 3 x 3 sub-boxes of the grid must contain the digits 1-9 without repetition.
Note:
A Sudoku board (partially filled) could be valid but is not necessarily solvable.
Only the filled cells need to be validated according to the mentioned rules.
*/
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
unordered_map<char, int> mp;
//each row
for(int i = 0; i < 9; i++){
mp.clear();
for(int j = 0; j < 9; j++){
if(board[i][j] == '.') continue;
mp[board[i][j]]++;
if(mp[board[i][j]] >= 2) return false;
}
}
//each column
for(int j = 0; j < 9; j++){
mp.clear();
for(int i = 0; i < 9; i++){
if(board[i][j] == '.') continue;
mp[board[i][j]]++;
if(mp[board[i][j]] >= 2) return false;
}
}
//3x3 box
for(int boxRow = 0; boxRow < 3; boxRow++){
for(int boxCol = 0; boxCol < 3; boxCol++){
mp.clear();
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
int r = boxRow * 3 + i;
int c = boxCol * 3 + j;
if(board[r][c] == '.') continue;
mp[board[r][c]]++;
if(mp[board[r][c]] >= 2) return false;
}
}
}
}
return true;
}
};