-
Notifications
You must be signed in to change notification settings - Fork 0
/
isValidSudoku.cpp
78 lines (71 loc) · 2.33 KB
/
isValidSudoku.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// https://leetcode.com/problems/valid-sudoku/
// Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
//
// The Sudoku board could be partially filled, where empty cells are filled
// with the character '.'.
//
// Note:
// A valid Sudoku board (partially filled) is not necessarily solvable. Only
// the filled cells need to be validated.
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
if(board.size() != 9)
return false;
unsigned char hash[10]; // value i hashed to the index i
// check each row
for(auto outer: board){
if(outer.size() != 9)
return false;
memset(hash, 0, 10);
for(auto inner: outer){
if(inner != '.'){
auto index = inner -'0';
if(hash[index] == 0)
hash[inner-'0']++;
else
return false;
}
}
}
// chech each column
unsigned char colval[9];
for(int col=0; col<9; col++){
memset(hash, 0, 10);
for(int row=0; row<9; row++)
colval[row] = board[row][col];
for(auto inner: colval){
if(inner != '.'){
auto index = inner -'0';
if(hash[index] == 0)
hash[index]++;
else
return false;
}
}
}
// chech each small square
for(int m=0; m<3; m++){
for(int n=0; n<3; n++){ // each tuple of (m,n) corresponds to a square
// assign value to square
memset(hash, 0, 10);
for(int i=0; i<3; i++){
for(int j=0; j<3; j++){
auto value = board[3*m+i][3*n+j];
if(value != '.'){
auto index = value -'0';
if(hash[index] == 0)
hash[index]++;
else
return false;
}
}
}
}
}
return true;
}
};
/* notes:
* Accepted at the first submit!
*/