您的位置:首页 > 其它

[leetcode] Valid Sudoku

2015-01-29 15:28 197 查看

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)
{
int i = 0, j = 0;
map<char, int> count;

//判断行是否合法
for(i = 0; i < 9; i++)
{
count.clear();
for(j = 0; j < 9; j++)
{
count[board[i][j]]++;
if(count[board[i][j]] > 1 && board[i][j] != '.')
return false;
}
}

//判断列是否合法
for(i = 0; i < 9; i++)
{
count.clear();
for(j = 0; j < 9; j++)
{
count[board[j][i]]++;
if(count[board[j][i]] > 1 && board[j][i] != '.')
return false;
}
}

// 判断九个小方块是否合法
for(i = 0; i < 9; i = i+3)
{
for(j = 0; j < 9; j = j+3)
{
count.clear();
for(int m = 0; m < 3; m++)
{
for(int n = 0; n < 3; n++)
{
count[board[i + m][j + n]]++;
if(count[board[i + m][j + n]] > 1 && board[i + m][j + n] != '.')
return false;
}
}
}
}

return true;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: