先给题目:
请你判断一个 9x9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。
数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)数独部分空格内已填入了数字,空白格用 '.' 表示。
注意:
一个有效的数独(部分已被填充)不一定是可解的。
只需要根据以上规则,验证已经填入的数字是否有效即可。示例 1:
输入:board =
[["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:
输入:board =
[["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 存在, 因此这个数独是无效的。提示:
board.length == 9
board[i].length == 9
board[i][j] 是一位数字或者 '.'
首先想到的肯定是暴力法直接遍历。
两重循环将九宫格里所有的元素全部遍历,
如果是点就不用寻找,直接continue跳过,
先按列寻找,遇见'.'就跳过,遇见数字比较一下,如果相等返回false,
再按行寻找,同理遇见'.'就跳过,然后比较一下,如果相等返回false,
接着将九宫格分为九个区域,
if(i<3&&j<3){...}
if(i>=3&&i<6&&j<3){...}
''''''
if(i<9&&i>=6&&j<9&&j>=6){...}
这个一定要注意,是从每个区域的头开始,例:if(i<3&&j<3){...}则从【0,0】开始到【3,3】
不断的遍历寻找除自身外是否还有与其相等的数字,
if(board[m1][m2]==board[i][j]&&m1!=i&&m2!=j)//m1,m2分别代表这个区域的行数和列数
return false;
有则返回false,没有继续遍历。
下面是暴力流的代码(好长一段):
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(board[i][j]=='.')
continue;
for(int k=i+1;k<9;k++)
{
if(board[k][j]=='.')
continue;
if(board[i][j]==board[k][j])
return false;
}
for(int l=j+1;l<9;l++)
{
if(board[i][l]=='.')
continue;
if(board[i][j]==board[i][l])
return false;
}
if(i<3&&j<3)
{ for(int m1=0;m1<3;m1++)
{
for(int m2=0;m2<3;m2++)
{
if(board[m1][m2]=='.')
continue;
if(board[m1][m2]==board[i][j]&&m1!=i&&m2!=j)
return false;
}
}
}
if(i>=3&&i<6&&j<3)
{ for(int m1=3;m1<6;m1++)
{
for(int m2=0;m2<3;m2++)
{
if(board[m1][m2]=='.')
continue;
if(board[m1][m2]==board[i][j]&&m1!=i&&m2!=j)
return false;
}
}
}
if(i>=6&&i<9&&j<3)
{ for(int m1=6;m1<9;m1++)
{
for(int m2=0;m2<3;m2++)
{
if(board[m1][m2]=='.')
continue;
if(board[m1][m2]==board[i][j]&&m1!=i&&m2!=j)
return false;
}
}
}
if(i<3&&j<6&&j>=3)
{ for(int m1=0;m1<3;m1++)
{
for(int m2=3;m2<6;m2++)
{
if(board[m1][m2]=='.')
continue;
if(board[m1][m2]==board[i][j]&&m1!=i&&m2!=j)
return false;
}
}
}
if(i<6&&i>=3&&j<6&&j>=3)
{ for(int m1=3;m1<6;m1++)
{
for(int m2=3;m2<6;m2++)
{
if(board[m1][m2]=='.')
continue;
if(board[m1][m2]==board[i][j]&&m1!=i&&m2!=j)
return false;
}
}
}
if(i<9&&i>=6&&j<6&&j>=3)
{ for(int m1=6;m1<9;m1++)
{
for(int m2=3;m2<6;m2++)
{
if(board[m1][m2]=='.')
continue;
if(board[m1][m2]==board[i][j]&&m1!=i&&m2!=j)
return false;
}
}
}
if(i<3&&j<9&&j>=6)
{ for(int m1=0;m1<3;m1++)
{
for(int m2=6;m2<9;m2++)
{
if(board[m1][m2]=='.')
continue;
if(board[m1][m2]==board[i][j]&&m1!=i&&m2!=j)
return false;
}
}
}
if(i<6&&i>=3&&j<9&&j>=6)
{ for(int m1=3;m1<6;m1++)
{
for(int m2=6;m2<9;m2++)
{
if(board[m1][m2]=='.')
continue;
if(board[m1][m2]==board[i][j]&&m1!=i&&m2!=j)
return false;
}
}
}
if(i<9&&i>=6&&j<9&&j>=6)
{ for(int m1=6;m1<9;m1++)
{
for(int m2=6;m2<9;m2++)
{
if(board[m1][m2]=='.')
continue;
if(board[m1][m2]==board[i][j]&&m1!=i&&m2!=j)
return false;
}
}
}
}
}
return true;
}
};
执行用时:24 ms, 在所有 C++ 提交中击败了64.75% 的用户
内存消耗:17.6 MB, 在所有 C++ 提交中击败了58.79% 的用户
结果居然还能接受。。。
不过既然要题解,肯定不能暴力直接过了,不然会讲我敷衍0.0
下面是思路:
1.由于board中的整数限定在1到9的范围内,因此可以分别建立哈希表来存储任一个数在相应维
度上是否出现过。维度有3个:所在的行,所在的列,所在的box,注意box的下标也是从左往右、
从上往下的。
2.遍历到每个数的时候,例如boar[i][j],我们判断其是否满足三个条件:
1.在第 i 个行中是否出现过
2.在第 j 个列中是否出现过
3.在第 j/3 + (i/3)*3个box中是否出现过
下面是代码:
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
int row[9][10] = {0};
int col[9][10] = {0};
int box[9][10] = {0};
for(int i=0; i<9; i++){
for(int j = 0; j<9; j++){
if(board[i][j] == '.') continue;
int curNumber = board[i][j]-'0';
if(row[i][curNumber]) return false;
if(col[j][curNumber]) return false;
if(box[j/3 + (i/3)*3][curNumber]) return false;
row[i][curNumber] = 1;
col[j][curNumber] = 1;
box[j/3 + (i/3)*3][curNumber] = 1;
}
}
return true;
}
};
执行用时:20 ms, 在所有 C++ 提交中击败了83.10% 的用户
内存消耗:17.4 MB, 在所有 C++ 提交中击败了88.55% 的用户
舒服多了。
计算机201 LW