389-判断数独是否合法
思路
暴力遍历
code
class Solution {
public:
/*
* @param board: the board
* @return: whether the Sudoku is valid
*/
bool isValidSudoku(vector<vector<char>> board) {
// write your code here
int sizeRow = board.size();
if (sizeRow != 9) {
return false;
}
int sizeCol = board[0].size();
if (sizeCol != 9) {
return false;
}
map<char, int> map;
for (int i = 0; i < sizeRow; i++) {
map.clear();
// 当前元素所在行
for (int j = 0; j < sizeCol; j++) {
if (board[i][j] <= '9' && board[i][j] >= '0') {
if (map[board[i][j]] > 0) {
return false;
}
else {
map[board[i][j]]++;
}
}
}
map.clear();
// 当前元素所在列
for (int j = 0; j < sizeRow; j++) {
if (board[j][i] <= '9' && board[j][i] >= '0') {
if (map[board[j][i]] > 0) {
return false;
}
else {
map[board[j][i]]++;
}
}
}
}
// 当前元素所在宫
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int row = i * 3, col = j * 3;
map.clear();
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
if (board[row + k][col + l] <= '9' && board[row + k][col + l] >= '0') {
if (map[board[row + k][col + l]] > 0) {
return false;
}
else {
map[board[row + k][col + l]]++;
}
}
}
}
}
}
return true;
}
};