-
Notifications
You must be signed in to change notification settings - Fork 1
/
36.有效的数独.cpp
57 lines (51 loc) · 1.44 KB
/
36.有效的数独.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
/*
* @lc app=leetcode.cn id=36 lang=cpp
*
* [36] 有效的数独
*/
// @lc code=start
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
bool st[9];
// 枚举行
for (int i = 0; i < 9; i++) {
memset(st, 0, sizeof st);
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
int t = board[i][j] - '1';
if (st[t]) return 0;
st[t] = 1;
}
}
}
// 枚举列
for (int i = 0; i < 9; i++) {
memset(st, 0, sizeof st);
for (int j = 0; j < 9; j++) {
if (board[j][i] != '.') {
int t = board[j][i] - '1';
if (st[t]) return 0;
st[t] = 1;
}
}
}
// 枚举 3*3
for (int i = 0; i < 9; i += 3) {
for (int j = 0; j < 9; j += 3) {
memset(st, 0, sizeof st);
for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
if (board[i + x][j + y] != '.') {
int t = board[i + x][j + y] - '1';
if (st[t]) return 0;
st[t] = 1;
}
}
}
}
}
return true;
}
};
// @lc code=end