題目敘述
Determine if a 9 x 9
Sudoku board is valid. Only the filled cells need to be validated according to the following rules:
- Each row must contain the digits
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 mentioned rules.
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.
限制:
board.length == 9
board[i].length == 9
board[i][j] is a digit 1-9 or '.'.
解題思路:
要檢查一個數獨裡面是否都是符合規範的方法就跟我們人在檢查的時候一樣。先檢查欄;之後檢查列;最後在看九宮格裡有沒有重複的。
所以這題就是要把每一欄每一列以及每一個九宮格和他們裡面存在的數字都當作一個 key-value pair 存在 Hash Map 裡面。
在遇到不是空格的數字就依序檢查 Hash Map 相對應的欄列裡面是否已經存在那個數字,如果已經存在就代表數獨是錯的。反之如果全部都沒有重複的話就代表數獨是正確的。
解題步驟:
- 宣告一個 Hash Map 用來存放每一欄、每一列、每一個九宮格出現過的數字
- 把所有格子都檢查一次,如果是空的就跳過,不是空的就去檢查 Hash Map 裡面該欄、列、九宮格是否出現已經存在那個數字
- 如果已經存在代表數獨是錯的回傳 false,如果全部檢查都還沒有錯就代表數獨正確回傳 true
Java 解法
class Solution {
private HashMap<String, ArrayList<Character>> existMap;
public boolean isValidSudoku(char[][] board) {
this.existMap = new HashMap<>();
for (int i = 0; i < board.length; i ++) {
for (int j = 0; j < board[i].length; j ++) {
char value = board[i][j];
// Only check if the cell is not empty
if (value == '.') continue;
// check if the value is already exist in the row and the column
if (!this.checkRow(i, value)) return false;
if (!this.checkColumn(j, value)) return false;
// get the index of the block and check if the value was exist inside the block
int blockIndex = this.getBlockIndex(i, j);
if (!this.checkBlock(blockIndex, value)) return false;
}
}
return true;
}
private boolean checkRow(int row, char value) {
String key = "R" + String.valueOf(row);
return this.checkNotInMap(key, value);
}
private boolean checkColumn(int column, char value) {
String key = "C" + String.valueOf(column);
return this.checkNotInMap(key, value);
}
private boolean checkBlock(int block, char value) {
String key = "B" + String.valueOf(block);
return this.checkNotInMap(key, value);
}
private boolean checkNotInMap(String key, char value) {
// if the key exist check if the value is exist in the array
if (this.existMap.containsKey(key)) {
ArrayList<Character> numbers = this.existMap.get(key);
if (numbers.contains(value)) return false;
numbers.add(value);
} else {
// if the key isn't exist, create the key-value pair
ArrayList<Character> numbers = new ArrayList<>();
numbers.add(value);
existMap.put(key, numbers);
}
return true;
}
private int getBlockIndex(int row, int column) {
int rowBlock = row / 3;
int columnBlock = column / 3;
return rowBlock * 3 + columnBlock;
}
}
Complexity
- Time Complexity: O(n);
- Space Complexity: O(n);