按照数独规则判断即可
代码:
1 bool isValidSudoku(vector<vector<char> > &board) { 2 unordered_set<char> record; 3 4 for (int i = 0; i < 9; i++) { 5 record.clear(); 6 for (int j = 0; j < 9; j++) { 7 char c = board[i][j]; 8 if (c == '.') 9 continue; 10 if (record.find(c) == record.end()) 11 record.insert(c); 12 else 13 return false; 14 } 15 } 16 17 for (int j = 0; j < 9; j++) { 18 record.clear(); 19 for (int i = 0; i < 9; i++) { 20 char c = board[i][j]; 21 if (c == '.') 22 continue; 23 if (record.find(c) == record.end()) 24 record.insert(c); 25 else 26 return false; 27 } 28 } 29 30 for (int i = 0; i < 9; i++) { 31 record.clear(); 32 for (int j = 0; j < 9; j++) { 33 char c = board[(i / 3) * 3 + j / 3][(i % 3) * 3 + j % 3]; 34 if (c == '.') 35 continue; 36 if (record.find(c) == record.end()) 37 record.insert(c); 38 else 39 return false; 40 } 41 } 42 43 return true; 44 }