bool isValidSudoku(vector<vector<char> > &board) { const int M = 9;//9 * 9 const int hash_len = 60;//'0' = 48 + 10 const char dot = '.'; //check rows for(int i = 0; i < M; ++i) { bool rows[hash_len] = {false}; bool cols[hash_len] = {false}; for(int j = 0; j < M; ++j){ if(board[i][j] != dot) { if(rows[board[i][j]]) return false; else rows[board[i][j]] = true; } } } //check cols for(int i = 0; i < M; ++i) { bool rows[hash_len] = {false}; for(int j = 0; j < M; ++j){ if(board[j][i] != dot) { if(rows[board[j][i]]) return false; else rows[board[j][i]] = true; } } } //check inner boxes. 9 * (3 * 3) const int N = 3; for(int ibox = 0; ibox < M; ++ibox) { bool box[hash_len] = {false}; for (int irow = 0; irow < N; ++irow) { for (int icol = 0; icol < N; ++icol) { if(board[ibox/N*N + irow][ibox%N*N + icol] != dot && box[board[ibox/N*N + irow][ibox%N*N + icol]]) return false; if(board[ibox/N*N + irow][ibox%N*N + icol] != dot) box[board[ibox/N*N + irow][ibox%N*N + icol]] = true; } } } return true; }
【leetcode】Valid Sudoku,布布扣,bubuko.com
原文地址:http://blog.csdn.net/shiquxinkong/article/details/28027205