解題思路
分成 row, column 以及九個 box,個別計算。
程式碼
class Solution { public: bool isValidSudoku(vector<vector<char>>& board) { int row[9][10] = {0}, col[9][10] = {0}, box[9][10] = {0}; for(int i=0; i<9; i++) { for(int j=0; j<9; j++) { if(board[i][j] == '.') continue; row[i][board[i][j] - '0']++; col[j][board[i][j] - '0']++; box[i/3*3 + j/3][board[i][j] - '0']++; if(row[i][board[i][j] - '0'] > 1 || col[j][board[i][j] - '0'] > 1 || box[i/3*3 + j/3][board[i][j] - '0'] > 1) { cout << row[i] << " " << col[j] << " " << box[i/3*3 + j/3] << endl; return false; } } } return true; } };
沒有留言:
張貼留言