-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path36.valid-sudoku.cpp
More file actions
47 lines (41 loc) · 1.29 KB
/
Copy path36.valid-sudoku.cpp
File metadata and controls
47 lines (41 loc) · 1.29 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
/*
* @lc app=leetcode id=36 lang=cpp
*
* [36] Valid Sudoku
*/
// @lc code=start
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
for (int i = 0; i < 9; ++i) {
int rowMask = 0, colMask = 0, boxMask = 0;
for (int j = 0; j < 9; ++j) {
int mask = 0, num = 0;
if (board[i][j] != '.') {
num = board[i][j] - '0';
mask = 1 << num;
if (rowMask & mask) return false;
rowMask |= mask;
}
mask = num = 0;
if (board[j][i] != '.') {
int num = board[j][i] - '0';
mask = 1 << num;
if (colMask & mask) return false;
colMask |= mask;
}
int rowIndex = 3 * (i / 3) + j / 3;
int colIndex = 3 * (i % 3) + j % 3;
mask = 0;
if (board[rowIndex][colIndex] != '.') {
int num = board[rowIndex][colIndex] - '0';
mask = 1 << num;
if (boxMask & mask) return false;
boxMask |= mask;
}
}
}
return true;
}
};
// @lc code=end