经典指数          
原因
964
浏览数
0
收藏数
 

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.

     举报   纠错  
 
切换
1 个答案
//使用一个bool[9]数组记录使用次数。 class Solution { public: bool isValidSudoku(vector > &board) { bool used[9]; for (int i = 0; i < 9; i++) { //先按行检查 memset(used, 0, 9); for (int j = 0; j < 9; j++) if (!check(board[i][j], used)) return false; //按列检查 memset(used, 0, 9); for (int j = 0; j < 9; j++) if (!check(board[j][i], used)) return false; } //从上到下,检查9方格 for (int x = 0; x < 3; x++) for (int y = 0; y < 3; y++) { //按行,从左到右检查3个9方格; memset(used, 0, 9); for (int i = x*3; i < x*3 + 3; i++) for (int j = y*3; j < y*3 + 3; j++) if (!check(board[i][j], used)) return false; } return true; } //private: //检查ch是否被使用,没用过返回true bool check(char ch, bool used[]) { if (ch == '.') return true; if (used[ch-'1']) return false; used[ch-'1'] = true; return true; } };
 
切换
撰写答案