标签:leetcode
链接:https://leetcode.com/problems/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.
Hide Tags Hash Table
验证数独是会否有效,即每一行,每一列,每个小方块内1-9这9个数字只能出现一次。其实判断起来很简单。但是需要搞清楚输入数据是怎么样的。输入数据是
vector<vector<char>>& board
board[i][j]表示第i行,j列的数据。
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board)
{
char table1[9][9],table2[9][9],table3[9][9];
memset(table1,0,81*sizeof(char));
memset(table2,0,81*sizeof(char));
memset(table3,0,81*sizeof(char));
for(int i=0;i<9;i++)
{
for(int j=0;j<9;j++)
{
if(board[i][j]>=‘1‘&&board[i][j]<=‘9‘)
{
if( table1[i/3*3+j/3][board[i][j]-‘1‘]==1 ||
table2[i][board[i][j]-‘1‘]==1 ||
table3[j][board[i][j]-‘1‘]==1)
return false;
table1[i/3*3+j/3][board[i][j]-‘1‘]=1;
table2[i][board[i][j]-‘1‘]=1;
table3[j][board[i][j]-‘1‘]=1;
}
}
}
return true;
}
};
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:leetcode
原文地址:http://blog.csdn.net/efergrehbtrj/article/details/46804961