判斷一個 9x9 的數獨是否有效。只需要根據以下規則,驗證已經填入的數字是否有效即可。
- 數字
1-9
在每一行只能出現一次。 - 數字
1-9
在每一列只能出現一次。 - 數字
1-9
在每一個以粗實線分隔的3x3
宮內只能出現一次。
<small style="box-sizing: border-box; font-size: 12px;">上圖是一個部分填充的有效的數獨。</small>
數獨部分空格內已填入了數字,空白格用 '.'
表示。
示例 1:
<pre style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; display: block; padding: 9.5px; margin: 0px 0px 10px; line-height: 1.42857; color: rgb(51, 51, 51); word-break: break-all; word-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">輸入:
[
["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"]
]
輸出: true
</pre>
示例 2:
<pre style="box-sizing: border-box; overflow: auto; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 13px; display: block; padding: 9.5px; margin: 0px 0px 10px; line-height: 1.42857; color: rgb(51, 51, 51); word-break: break-all; word-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-radius: 4px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px;">輸入:
[
["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"]
]
輸出: false
解釋: 除了第一行的第一個數字從 5 改為 8 以外,空格內其他數字均與 示例1 相同。
但由于位于左上角的 3x3 宮內有兩個 8 存在, 因此這個數獨是無效的。</pre>
說明:
- 一個有效的數獨(部分已被填充)不一定是可解的。
- 只需要根據以上規則,驗證已經填入的數字是否有效即可。
- 給定數獨序列只包含數字
1-9
和字符'.'
。 - 給定數獨永遠是
9x9
形式的。
思路
分為三步,對行的判斷,把是數字的部分加入map,判斷是否重復,列同理,對九宮格的判斷,一共有9個九宮格,一個個進行判斷
實現代碼
class Solution {
public boolean isValidSudoku(char[][] board) {
for(int i=0;i<9;++i){
HashMap<Character,Integer> map=new HashMap<Character,Integer>();
// HashSet<Character> set = new HashSet<Character>();
for(int j=0;j<9;++j){
if(board[i][j]!='.'){
if(map.containsKey(board[i][j])){
return false;
}
map.put(board[i][j],j);
}
}
}
for(int j=0;j<9;++j){
HashMap<Character,Integer> map=new HashMap<Character,Integer>();
for(int i=0;i<9;++i){
if(board[i][j]!='.'){
if(map.containsKey(board[i][j])){
return false;
}
map.put(board[i][j],j);
}
}
}
for(int i = 0; i<7; i+= 3){
for(int j = 0; j<7; j+= 3){
HashMap<Character,Integer> map=new HashMap<Character,Integer>();
HashSet<Character> set = new HashSet<Character>();
for(int m = i; m < i + 3;m++){
for(int n = j; n < j+3; n++){
// if(board[m][n]!='.' && map.containsKey(board[m][n])){
// return false;
// }
// map.put(board[m][n],m);
if(board[m][n]!='.'){
if(map.containsKey(board[m][n])){
return false;
}
map.put(board[m][n],j);
}
}
}
}
}
return true;
}
}