Valid Sudoku (Easy)

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 ‘.’.

Analysis

判断一个数独是否是合法的。直接hash行,列以及每个九宫格判断是否重复即可

My Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//C++
class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
int row[9][9],col[9][9],sq[9][9];
memset(row,0,sizeof(row));
memset(col,0,sizeof(col));
memset(sq,0,sizeof(sq));
for(int i = (int)board.size()-1; i>=0 ; i--)
{
for(int j = board[i].size()-1; j>=0 ; j--)
{
if(!(board[i][j]>='1'&&board[i][j]<='9'))
continue;
int x = board[i][j]-'1';
int y = i/3*3+j/3;
if(row[i][x]||col[j][x]||sq[y][x])
return false;
row[i][x]=col[j][x]=sq[y][x]=1;
}
}
return true;
}
};