class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
int x=3;
int y=3;
int tx=0;
int ty=0;
for(int i=0;i<9;i++)
{
int a[10];
memset(a,0,sizeof(a)) ;
int b[10];
memset(b,0,sizeof(b)) ;
for(int j=0;j<9;j++)
{
if(board[j][i]!='.')
{
int p=board[j][i]-'0';
if(b[p]==0)
{
b[p]=1;
}
else
{
return 0;
}
}
if(board[i][j]!='.')
{
int q=board[i][j]-'0';
if(a[q]==0)
{
a[q]=1;
}
else
{
return 0;
}
}
}
}
int c[10];
memset(c,0,sizeof(c)) ;
while(x<=9)
{
for(tx=x-3;tx<x;++tx)
{
for(ty=y-3;ty<y;++ty)
{
if(board[tx][ty]!='.')
{
if(c[board[tx][ty]-'0']==0)
{
c[board[tx][ty]-'0']=1;
}
else
{
return 0;
}
}
}
}
memset(c,0,sizeof(c)) ;
if(y<9)
{
y+=3;
}
else
{
y=3;
x+=3;
}
}
return 1;
}
};
又是纯暴力的一天。
不过这一题花了两个小时。很烦。中间debug一度想要放掉。
主要的思路和代码不到一个小时就结束了。
就因为几个点一直没找到所以浪费了好多时间。
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
int rows[9][9];
int columns[9][9];
int subboxes[3][3][9];
memset(rows,0,sizeof(rows));
memset(columns,0,sizeof(columns));
memset(subboxes,0,sizeof(subboxes));
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
char c = board[i][j];
if (c != '.') {
int index = c - '0' - 1;
rows[i][index]++;
columns[j][index]++;
subboxes[i / 3][j / 3][index]++;
if (rows[i][index] > 1 || columns[j][index] > 1 || subboxes[i / 3][j / 3][index] > 1) {
return false;
}
}
}
}
return true;
}
};
同样是遍历,答案的代码就好看很多了。
T.T
加油!