LeetCode 36. Valid Sudoku 判斷9*9的數獨板是否有效
阿新 • • 發佈:2019-01-28
Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits
1-9
without repetition. - Each column must contain the digits
1-9
without repetition. - Each of the 9
3x3
sub-boxes of the grid must contain the digits1-9
A partially filled sudoku which is valid.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
Example 1:
Input: [ ["5","3",".",".","7",".",".",".","."], ["6",".",".","1","9","5",".",".","."], [".","9","8",".",".",".",".","6","."], ["8",".",".",".","6",".",".",".","3"], ["4",".",".","8",".","3",".",".","1"], ["7",".",".",".","2",".",".",".","6"], [".","6",".",".",".",".","2","8","."], [".",".",".","4","1","9",".",".","5"], [".",".",".",".","8",".",".","7","9"] ] Output:true
Example 2:
Input: [ ["8","3",".",".","7",".",".",".","."], ["6",".",".","1","9","5",".",".","."], [".","9","8",".",".",".",".","6","."], ["8",".",".",".","6",".",".",".","3"], ["4",".",".","8",".","3",".",".","1"], ["7",".",".",".","2",".",".",".","6"], [".","6",".",".",".",".","2","8","."], [".",".",".","4","1","9",".",".","5"], [".",".",".",".","8",".",".","7","9"] ] Output:false Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Note:
- A Sudoku board (partially filled) could be valid but is not necessarily solvable.
- Only the filled cells need to be validated according to the mentioned rules.
- The given board contain only digits
1-9
and the character'.'
. - The given board size is always
9x9
.
方法一 藉助set的特性分別判斷行,列,子快是否有效
藉助set無重複元素的特性來輔助判斷。
class Solution {
public:
static const set<char> num;
bool isValidSudoku(vector<vector<char>>& board) {
set<char> record;
bool isValid = true;
if (board.size() != 9 || board[0].size() != 9) {
return false;
}
//先搜尋所有行是否滿足
for (auto a : board) {
record.clear();
for (auto b : a) {
if (b == '.') {
continue;
}
if (num.find(b) == num.end()) {
return false;
}
if (record.find(b) != record.end()) {
isValid = false;
return isValid;
}
else {
record.insert(b);
}
}
}
//搜尋所有列是否滿足
for (int i = 0; i < 9; ++i) {
record.clear();
for (int j = 0; j < 9; ++j) {
if (board[j][i] == '.') {
continue;
}
if (num.find(board[j][i]) == num.end()) {
return false;
}
if (record.find(board[j][i]) != record.end()) {
isValid = false;
return isValid;
}
else {
record.insert(board[j][i]);
}
}
}
//判斷9個3*3的子快是否滿足
for (int m = 0; m <= 6; m += 3) {
for (int n = 0; n <= 6; n += 3) {
record.clear();
for (int i = m; i < m + 3; ++i) {
for (int j = n; j < n + 3; ++j) {
if (board[i][j] == '.') {
continue;
}
if (num.find(board[i][j]) == num.end()) {
return false;
}
if (record.find(board[i][j]) != record.end()) {
isValid = false;
return isValid;
}
else {
record.insert(board[i][j]);
}
}
}
}
}
return isValid;
}
};
const set<char> Solution::num = { '1','2' ,'3' ,'4' ,'5' ,'6' ,'7' ,'8' ,'9' };
方法二:建立三個輔助二維陣列來判斷行、列、子快是否有效
Three flags are used to check whether a number appear.
used1: check each row
used2: check each column
used3: check each sub-boxes
class Solution
{
public:
bool isValidSudoku(vector<vector<char> > &board)
{
int used1[9][9] = {0}, used2[9][9] = {0}, used3[9][9] = {0};
for(int i = 0; i < board.size(); ++ i)
for(int j = 0; j < board[i].size(); ++ j)
if(board[i][j] != '.')
{
int num = board[i][j] - '0' - 1, k = i / 3 * 3 + j / 3;
if(used1[i][num] || used2[j][num] || used3[k][num])
return false;
used1[i][num] = used2[j][num] = used3[k][num] = 1;
}
return true;
}
};
時間複雜度:O(n2)