Valid Sudoku 题解
原创文章,拒绝转载
题目来源:https://leetcode.com/problems/valid-sudoku/description/
Description
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 '.'
.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
Solution
class Solution {
public:
bool isValidSudoku(vector<vector<char> >& board) {
int row, col;
for (row = 0; row < 9; row++) {
vector<bool> bucket(9, false);
for (col = 0; col < 9; col++) {
if (board[row][col] != '.') {
if (!bucket[board[row][col] - '1'])
bucket[board[row][col] - '1'] = true;
else
return false;
}
}
}
for (col = 0; col < 9; col++) {
vector<bool> bucket(9, false);
for (row = 0; row < 9; row++) {
if (board[row][col] != '.') {
if (!bucket[board[row][col] - '1'])
bucket[board[row][col] - '1'] = true;
else
return false;
}
}
}
int rstart, cstart;
for (rstart = 0; rstart < 9; rstart += 3) {
for (cstart = 0; cstart < 9; cstart += 3) {
vector<bool> bucket(9, false);
for (row = rstart; row < rstart + 3; row++) {
for (col = cstart; col < cstart + 3; col++) {
if (board[row][col] != '.') {
if (!bucket[board[row][col] - '1'])
bucket[board[row][col] - '1'] = true;
else
return false;
}
}
}
}
}
return true;
}
};
解题描述
这道题题意非常简单,就是求给出的半满九宫格是不是可以完成的。直接使用了暴力查找解决。