Leetcode 36:有效的数独
题目描述
判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。
数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。
上图是一个部分填充的有效的数独。
数独部分空格内已填入了数字,空白格用 ‘.’ 表示。
示例 1:
输入:
[
[“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”]
]
输出: true
示例 2:
输入:
[
[“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”]
]
输出: false
解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。
但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
说明:
- 一个有效的数独(部分已被填充)不一定是可解的。
- 只需要根据以上规则,验证已经填入的数字是否有效即可。
- 给定数独序列只包含数字 1-9 和字符 ‘.’ 。
- 给定数独永远是 9x9 形式的。
我的解法:利用set特性,将判断分为三个步骤
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
set<char> s;
//行重复检测
for (int m = 0; m < 9; m++){
for (int n = 0; n < 9; n++){
if (board[m][n] != '.'){
if (s.count(board[m][n])){ return false; }
else{ s.insert(board[m][n]); }
}
}
s.clear();
}
//列重复检测
for (int n = 0; n < 9; n ++){
for (int m = 0; m < 9; m++){
if (board[m][n] != '.'){
if (s.count(board[m][n])){ return false; }
else{ s.insert(board[m][n]); }
}
}
s.clear();
}
int m0, m1, n0, n1;
//块重复检测
for (int m = 0; m < 9; m++){
for (int n = 0; n < 9; n++){
if (board[m][n] != '.'){
if (m % 3 == 0){ m0 = m + 1; m1 = m + 2; }
else if (m % 3 == 1){ m0 = m - 1; m1 = m + 1; }
else{ m0 = m - 2; m1 = m - 1; }
if (n % 3 == 0){ n0 = n + 1; n1 = n + 2; }
else if (n % 3 == 1){ n0 = n - 1; n1 = n + 1; }
else{ n0 = n - 2; n1 = n - 1; }
if (m0 > m){
if (board[m0][n0] != '.'){ s.insert(board[m0][n0]); }
if (board[m0][n1] != '.'){ s.insert(board[m0][n1]); }
}
if (m1 > m){
if (board[m1][n0] != '.'){ s.insert(board[m1][n0]); }
if (board[m1][n1] != '.'){ s.insert(board[m1][n1]); }
}
if (s.count(board[m][n])){ return false; }
s.clear();
}
}
}
return true;
}
};
虽然程序执行效果还不错,运行时间和空间均超过了百分之80多的解答,但是代码比较复杂,可读性差,下面是别人家的改进解法。
别人家的解法:用算法代替了多个if定义
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
vector<unordered_set<int>> row(9);
vector<unordered_set<int>> col(9);
vector<unordered_set<int>> block(9);
for(int i = 0; i < 9; ++ i){
for(int j = 0; j < 9; ++ j){
int bindex = (i / 3)* 3 + j / 3;
char cur = board[i][j];
if(cur == '.') continue;
if(row[i].count(cur) || col[j].count(cur) || block[bindex].count(cur)) return false;
row[i].insert(cur);
col[j].insert(cur);
block[bindex].insert(cur);
}
}
return true;
}
};
可以看到,将判断合三为一,大大减少了代码量。特别注意以下两点:
- set、hash等容器都可以定义容器数组,此操作省去了连续定义多个容器的问题。
- 注意这个代码:bindex = (i / 3)* 3 + j / 3,它的功能是定位到元素所在的9格块,此思想解决了我的代码中定义了一堆if的问题。