Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:html
1-9
without repetition.1-9
without repetition.3x3
sub-boxes of the grid must contain the digits 1-9
without repetition.
A partially filled sudoku which is valid.git
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.github
Example 1:數組
Input: [ ["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:post
Input: [ ["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.
Note:優化
1-9
and the character '.'
.9x9
.
這道題讓驗證一個方陣是否爲數獨矩陣,想必數獨遊戲咱們都玩過,就是給一個 9x9 大小的矩陣,能夠分爲9個 3x3 大小的矩陣,要求是每一個小矩陣內必須都是1到9的數字不能有重複,同時大矩陣的橫縱列也不能有重複數字,是一種很經典的益智類遊戲,常常在飛機上看見有人拿着紙筆在填數,感受是消磨時光的利器。這道題給了一個殘缺的二維數組,讓咱們判斷當前的這個數獨數組是否合法,即要知足上述的條件。判斷標準是看各行各列是否有重複數字,以及每一個小的 3x3 的小方陣裏面是否有重複數字,若是都無重複,則當前矩陣是數獨矩陣,但不表明待數獨矩陣有解,只是單純的判斷當前未填完的矩陣是不是數獨矩陣。那麼根據數獨矩陣的定義,在遍歷每一個數字的時候,就看看包含當前位置的行和列以及 3x3 小方陣中是否已經出現該數字,這裏須要三個 boolean 型矩陣,大小跟原數組相同,分別記錄各行,各列,各小方陣是否出現某個數字,其中行和列標誌下標很好對應,就是小方陣的下標須要稍稍轉換一下,具體代碼以下:編碼
解法一:url
class Solution { public: bool isValidSudoku(vector<vector<char>>& board) { vector<vector<bool>> rowFlag(9, vector<bool>(9)); vector<vector<bool>> colFlag(9, vector<bool>(9)); vector<vector<bool>> cellFlag(9, vector<bool>(9)); for (int i = 0; i < 9; ++i) { for (int j = 0; j < 9; ++j) { if (board[i][j] == '.') continue; int c = board[i][j] - '1'; if (rowFlag[i][c] || colFlag[c][j] || cellFlag[3 * (i / 3) + j / 3][c]) return false; rowFlag[i][c] = true; colFlag[c][j] = true; cellFlag[3 * (i / 3) + j / 3][c] = true; } } return true; } };
咱們也能夠對空間進行些優化,只使用一個 HashSet 來記錄已經存在過的狀態,將每一個狀態編碼成爲一個字符串,能將如此大量信息的狀態編碼成一個單一的字符串仍是須要有些技巧的。對於每一個1到9內的數字來講,其在每行每列和每一個小區間內都是惟一的,將數字放在一個括號中,每行上的數字就將行號放在括號左邊,每列上的數字就將列數放在括號右邊,每一個小區間內的數字就將在小區間內的行列數分別放在括號的左右兩邊,這樣每一個數字的狀態都是獨一無二的存在,就能夠在 HashSet 中愉快地查找是否有重複存在啦,參見代碼以下:spa
解法二:code
class Solution { public: bool isValidSudoku(vector<vector<char>>& board) { unordered_set<string> st; for (int i = 0; i < 9; ++i) { for (int j = 0; j < 9; ++j) { if (board[i][j] == '.') continue; string t = "(" + to_string(board[i][j]) + ")"; string row = to_string(i) + t, col = t + to_string(j), cell = to_string(i / 3) + t + to_string(j / 3); if (st.count(row) || st.count(col) || st.count(cell)) return false; st.insert(row); st.insert(col); st.insert(cell); } } return true; } };
Github 同步地址:
https://github.com/grandyang/leetcode/issues/36
相似題目:
參考資料:
https://leetcode.com/problems/valid-sudoku/
https://leetcode.com/problems/valid-sudoku/discuss/15450/Shared-my-concise-Java-code
https://leetcode.com/problems/valid-sudoku/discuss/15472/Short%2BSimple-Java-using-Strings