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 tmp = board[i][j] - '1';
if(st[tmp]) return false;
st[tmp] = true;
}
}
}
// 判断列
for(int i = 0; i < 9; i ++)
{
memset(st, 0, sizeof st);
for(int j = 0; j < 9; j ++)
{
if(board[j][i] != '.')
{
int tmp = board[j][i] - '1';
if(st[tmp]) return false;
st[tmp] = true;
}
}
}
// 判断九宫格
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 tmp = board[i + x][j + y] - '1';
if(st[tmp]) return false;
st[tmp] = true;
}
}
}
return true;
}
};
LeetCode 36. 有效的数独
最新推荐文章于 2024-12-12 00:58:01 发布