Valid Sudoku
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 '.'.
![]()
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
代码不简洁,暴力解了。
class Solution {
public:
bool isValidSudoku(vector<vector<char> > &board) {
for(int i = 0 ; i < 9 ; i++)
{
map<char , int> m;
for(int j = 0; j <9 ;j++)
{
char temp =board[i][j];
//if((temp > '9' || temp <'0' ) && temp !='.')return 0;
if(temp != '.')
{
if(m.find(temp)!=m.end()) return 0;
else m[temp] = 1;
}
}
}
for(int i = 0 ; i < 9 ; i++)
{
map<char , int> m;
for(int j = 0; j <9 ;j++)
{
char temp =board[j][i];
// if((temp > '9' || temp <'0' ) && temp !='.')return 0;
if(temp != '.')
{
if(m.find(temp)!=m.end()) return 0;
else m[temp] = 1;
}
}
}
for(int i = 0 ; i < 3 ; i++)
for(int j = 0 ; j < 3 ; j++)
{
map<char , int> m;
for(int k = 0 ; k < 3; k++)
{
for(int l = 0 ; l < 3 ; l++)
{
char temp = board[i*3+k][j*3+l];
//if((temp > '9' || temp <'0' ) && temp !='.')return 0;
if(temp != '.')
{
if(m.find(temp)!=m.end()) return 0;
else m[temp] = 1;
}
}
}
}
return 1;
}
};
posted on 2014-02-28 15:13 pengyu2003 阅读(148) 评论(0) 收藏 举报
浙公网安备 33010602011771号