36. Valid Sudoku
阿新 • • 發佈:2020-10-18
Determine if a9 x 9
Sudoku boardis valid.Only the filled cells need to be validatedaccording to the following rules:
- Each rowmust contain thedigits
1-9
without repetition. - Each column must contain the digits
1-9
without repetition. - Each of the nine
3 x 3
sub-boxes of the grid must contain the digits1-9
without repetition.
Note:
- A Sudoku board (partially filled) could be valid but is not necessarily solvable.
- Only the filled cells need to be validated according to the mentionedrules.
Example 1:
Input: board = [["5","3",".",".","7",".",".",".","."] ,["6",".",".","1","9","5",".",".","."] ,[".","9","8",".",".",".",".","6","."] ,["8",".",".",".","6",".",".",".","3"] ,["4",".",".","8",".","3",".",".","1"] ,["7",".",".",".","2",".",".",".","6"] ,[".","6",".",".",".",".","2","8","."] ,[".",".",".","4","1","9",".",".","5"] ,[".",".",".",".","8",".",".","7","9"]] Output: true
Example 2:
Input: board = [["8","3",".",".","7",".",".",".","."] ,["6",".",".","1","9","5",".",".","."] ,[".","9","8",".",".",".",".","6","."] ,["8",".",".",".","6",".",".",".","3"] ,["4",".",".","8",".","3",".",".","1"] ,["7",".",".",".","2",".",".",".","6"] ,[".","6",".",".",".",".","2","8","."] ,[".",".",".","4","1","9",".",".","5"] ,[".",".",".",".","8",".",".","7","9"]] Output: false Explanation: Same as Example 1, except with the 5 in the top left corner being modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.
Constraints:
board.length == 9
board[i].length == 9
board[i][j]
is a digit or'.'
.
class Solution { public: //核心:輸入的每一行、每一列、每一個3*3 box只出現一次1-9中的某個數字. //利用某個set 來分別區分也可以。本質上是搞個標誌。 bool isValidSudoku(vector<vector<char>>& board) { //判定每一行數字出現情況.第二個9表示數字0-8 bool used1[9][9] = {false}; //判定每一列數字出現情況,第二個9表示數0-8 bool used2[9][9] = {false}; //判定每個3*3 box數字出現情況,第一個9表示0-8個box bool used3[9][9] = {false}; for(int i=0;i<board.size();i++){ for(int j=0;j<board[i].size();j++){ if(board[i][j] == '.') continue; int num = board[i][j]-'0'-1; if(num < 0 || num > 8) return false; //第i行或者第j列出現過.used陣列只是標誌陣列 if(used1[i][num] || used2[j][num]) return false; used1[i][num] = true; used2[j][num] = true; //目前的board[i][j]在第幾個box(00 01 02、10 11 12、20 21 22) int box = (i/3)*3+j/3; if(used3[box][num]) return false; used3[box][num] = true; } } return true; } };
//法二也很巧妙:利用三類標誌
class Solution { public: //利用set來區分。 //假設第1行出現3即表示為 (1)3 //假設第1列出現3即表示為 3(1) //9個box中,i/3 與 j/3組合共有9個分別 //00 01 02 //10 11 12 //20 21 22 //那麼第一個box出現了3表示為 0(3)0. bool isValidSudoku(vector<vector<char>>& board) { set<string> SudoRow; set<string> SudoCol; set<string> SudoBox; //boad[i][j]是char for(int i=0;i<board.size();i++){ for(int j=0;j<board[i].size();j++){ if(board[i][j] == '.') continue; if(board[i][j]-'0'-1 < 0 || board[i][j]-'0'-1 > 8) return false; string row = "("+to_string(i)+")"+board[i][j]; string col = to_string(j)+"("+board[i][j]+")"; string box = to_string(i/3)+"("+board[i][j]+")"+to_string(j/3); if(SudoRow.find(row) != SudoRow.end() || SudoCol.find(col) != SudoCol.end() || SudoBox.find(box) != SudoBox.end()){ return false; } SudoRow.insert(row); SudoCol.insert(col); SudoBox.insert(box); } } return true; } };