您的位置:首页 > 其它

LeetCode 36. Valid Sudoku

2016-07-24 15:16 281 查看
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) {
map<char, int> m;
int i, j;
for(i = 0; i < 9; i ++){
m.clear();
for(j = 0; j < 9; j ++){
if(board[i][j] != '.'){
if(m[board[i][j]] == 1) return false;
m[board[i][j]] = 1;
}
}
}
for(i = 0; i < 9; i ++){
m.clear();
for(j = 0; j < 9; j ++){
if(board[j][i] != '.'){
if(m[board[j][i]] == 1) return false;
m[board[j][i]] = 1;
}
}
}
for(i = 0; i < 7; i += 3){
for(j = 0; j < 7; j += 3){
int k, t;
m.clear();
for(k = i; k < 3 + i; k ++){
for(t = j; t < 3 + j; t ++){
if(board[k][t] != '.'){
if(m[board[k][t]] == 1) return false;
m[board[k][t]] = 1;
}
}
}
}
}
return true;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: