[LeetCode]Valid Sudoku
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.
判断9行,9列,和3*3的数据块。
1 class Solution { 2 public: 3 bool isValidSudoku(vector<vector<char>>& board) { 4 unordered_map<char,int> showed; 5 for(int i=0;i<9;i++) 6 { 7 showed.clear(); 8 for(int j=0;j<9;j++) 9 { 10 if(board[i][j]=='.') continue; 11 if(showed.find(board[i][j])!=showed.end()) 12 { 13 return false; 14 } 15 else 16 { 17 showed[board[i][j]]=1; 18 } 19 } 20 showed.clear(); 21 for(int j=0;j<9;j++) 22 { 23 if(board[j][i]=='.') continue; 24 if(showed.find(board[j][i])!=showed.end()) 25 { 26 return false; 27 } 28 else 29 { 30 showed[board[j][i]]=1; 31 } 32 } 33 } 34 for(int i=0;i<3;i++) 35 { 36 for(int j=0;j<3;j++) 37 { 38 showed.clear(); 39 for(int m=0;m<3;m++) 40 { 41 for(int n=0;n<3;n++) 42 { 43 if(board[i*3+m][j*3+n]=='.') continue; 44 if(showed.find(board[i*3+m][j*3+n])!=showed.end()) 45 { 46 return false; 47 } 48 else 49 { 50 showed[board[i*3+m][j*3+n]]=1; 51 } 52 } 53 } 54 } 55 } 56 return true; 57 } 58 };