LeetCode 36. 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 '.'
.
大意就是给你一个不完整的数独,让你判断是否符合数独规范,水题。
代码如下:
class Solution {
public:
bool isValidSudoku(vector<vector<char>>& board) {
bool row[10][10], val[10][10], rtl[10][10];
memset(row, false, sizeof(row));
memset(val, false, sizeof(val));
memset(rtl, false, sizeof(rtl));
for(int i = 0; i < 9; ++ i)
{
for(int j = 0; j < 9; ++ j)
{
if(board[i][j] == '.')
continue;
int t = board[i][j] - '0';
if(row[i][t] || val[j][t] || rtl[i / 3 * 3 + j / 3][t])
return false;
row[i][t] = val[j][t] = true;
rtl[i / 3 * 3 + j / 3][t] = true;
}
}
return true;
}
};