LeetCode題目鏈接:https://leetcode-cn.com/problems/valid-sudoku/
此題目在面試便利蜂的時候筆試時遇到了,當時沒有完全做出來,現在整理如下。
題目:
判斷一個 9x9 的數獨是否有效。只需要根據以下規則,驗證已經填入的數字是否有效即可。
數字 1-9 在每一行只能出現一次。
數字 1-9 在每一列只能出現一次。
數字 1-9 在每一個以粗實線分隔的 3x3 宮內只能出現一次。
上圖是一個部分填充的有效的數獨。
數獨部分空格內已填入了數字,空白格用 '.' 表示。
示例 1:
輸入:
[
["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
示例 2:
輸入:
[
["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 存在, 因此這個數獨是無效的。
Java解數獨
分析:
1、我們可以看做是一個二維數組,分別判斷每一行、每一列、每一個小塊中是否有重複的數('.'除外),有重複則返回false,沒有則返回true。
2、先判斷一個一維數組中是否存在相同的數
3、拿到每一行,每一列,每一個小快組成一個數組,調用2方法判斷
public class TestSuDu {
//LeetCode 36題數獨
public static void main(String[] args) {
long start = System.nanoTime();
char[][] t = {
{'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'}};
System.out.println(isValidSUdoku(t));
long end = System.nanoTime();
System.out.println(end - start);//納秒
System.out.println((end - start) / 1000000L);//毫秒
}
public static int[] range(int start, int end, int step) {
int sz = (end - start) / step;
int[] result = new int[sz];
for (int i = 0; i < sz; i++) result[i] = start + (i * step);
return result;
}
public static boolean isValidSUdoku(char[][] board) {
if (board == null) {
return false;
}
for (int i = 0; i < board.length; i++) {
if (board[i] == null) {
return false;
}
//判斷每一行
if (!istrue(board[i])) {
return false;
}
//判斷每一列
char[] temp = new char[9];
for (int k = 0; k < board.length; k++) {
temp[k] = board[k][i];
}
if (!istrue(temp)) {
return false;
}
}
int[] ints = range(0, 9, 3);
for (int anInt : ints) {
for (int i : ints) {
char[] temp = new char[9];
int index = 0;
for (int k = anInt; k < anInt + 3; k++) {
for (int q = i; q < i + 3; q++) {
temp[index] = board[k][q];
index++;
}
}
if (!istrue(temp)) {
return false;
}
}
}
return true;
}
public static boolean istrue(char[] line) {
if (line == null) {
return false;
}
int index = 0;
Set set = new HashSet();
for (int i = 0; i < line.length; i++) {
if (line[i] != '.') {
set.add(line[i]);
index++;
}
}
if (index == set.size()) {
return true;
} else {
return false;
}
}
}
在leetcode上提交結果如下: