题目:
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 '.'
.
思路: 做三个判断: 行, 列, block
代码:
1 class Solution { 2 public: 3 bool isValidSudoku(vector<vector<char> > &board) { 4 5 vector<vector<bool> > rows(9, vector<bool>(9, false)); 6 vector<vector<bool> > cols(9, vector<bool>(9, false)); 7 vector<vector<bool> > blocks(9, vector<bool>(9, false)); 8 9 for (int i = 0; i < 9; ++i) { 10 for (int j = 0; j < 9; ++j) { 11 if (board[i][j] == '.') continue; 12 int c = board[i][j] - '1'; 13 if (rows[i][c] || cols[j][c] || blocks[i - i % 3 + j / 3][c]) 14 return false; 15 rows[i][c] = cols[j][c] = blocks[i - i % 3 + j / 3][c] = true; 16 } 17 } 18 return true; 19 } 20 };