Determine if a Sudoku is valid, according to: .
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
A partially filled sudoku which is valid.
思路:
思路很简单,只要检查每行、每列、每个子区域中没有重复的数字即可。
代码:
1 bool isValidSudoku(vector> &board) { 2 // Note: The Solution object is instantiated only once and is reused by each test case. 3 map table; 4 for(int i = 0; i < 9; i++){ 5 table.clear(); 6 for(int j = 0; j < 9; j++){ 7 if(board[i][j] != '.'){ 8 if(table.find(board[i][j]) != table.end()) 9 return false;10 else11 table[board[i][j]] = 1;12 }13 }14 }15 for(int i = 0; i < 9; i++){16 table.clear();17 for(int j = 0; j < 9; j++){18 if(board[j][i] != '.'){19 if(table.find(board[j][i]) != table.end())20 return false;21 else22 table[board[j][i]] = 1;23 }24 }25 }26 for(int i = 0; i < 9; i++){27 table.clear();28 for(int j = 0; j < 3; j++){29 for(int k = 0; k < 3; k++){30 if(board[3*(i/3)+j][3*(i%3)+k] != '.'){31 if(table.find(board[3*(i/3)+j][3*(i%3)+k]) != table.end())32 return false;33 else34 table[board[3*(i/3)+j][3*(i%3)+k]] = 1;35 }36 }37 }38 }39 return true;40 }