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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
class Solution { public: bool isValidSudoku(vector<vector<char>>& board) { int book[10]; memset(book, 0, sizeof(int)*10); //每一列 for(int i = 0; i < 9; i++) { for(int j = 0; j < 9; j++) { if(board[i][j] == '.') continue; if(book[board[i][j] - '0'] == 0) book[board[i][j] - '0'] = 1; else return false; } memset(book, 0, sizeof(int) * 10); } //每一行 for(int i = 0; i < 9; i++) { for(int j = 0; j < 9; j++) { if(board[j][i] == '.') continue; if(book[board[j][i] - '0'] == 0) book[board[j][i] - '0'] = 1; else return false; } memset(book, 0, sizeof(int) * 10); } //每个小九宫格 for(int m = 0; m <= 6; m = m + 3) { for(int i = 0; i <= 8; i++) { if(i % 3 == 0) memset(book, 0, sizeof(int) * 10); for(int j = 0; j <= 2; j++) { if(board[i][j + m] == '.') continue; if(book[board[i][j + m] - '0'] == 0) book[board[i][j + m] - '0'] = 1; else return false; } } } return true; } }; |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼