判斷一個 9x9 的數(shù)獨(dú)是否有效。只需要根據(jù)以下規(guī)則,驗證已經(jīng)填入的數(shù)字是否有效即可。
- 數(shù)字
1-9
在每一行只能出現(xiàn)一次息尺。 - 數(shù)字
1-9
在每一列只能出現(xiàn)一次。 - 數(shù)字
1-9
在每一個以粗實(shí)線分隔的3x3
宮內(nèi)只能出現(xiàn)一次疾掰。
上圖是一個部分填充的有效的數(shù)獨(dú)搂誉。
數(shù)獨(dú)部分空格內(nèi)已填入了數(shù)字,空白格用 '.'
表示静檬。
示例 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
解釋: 除了第一行的第一個數(shù)字從 5 改為 8 以外炭懊,空格內(nèi)其他數(shù)字均與 示例1 相同。
但由于位于左上角的 3x3 宮內(nèi)有兩個 8 存在, 因此這個數(shù)獨(dú)是無效的拂檩。
說明:
- 一個有效的數(shù)獨(dú)(部分已被填充)不一定是可解的侮腹。
- 只需要根據(jù)以上規(guī)則,驗證已經(jīng)填入的數(shù)字是否有效即可稻励。
- 給定數(shù)獨(dú)序列只包含數(shù)字
1-9
和字符'.'
父阻。 - 給定數(shù)獨(dú)永遠(yuǎn)是
9x9
形式的。
我自己給的算法:
public static boolean isValidSudoku(char[][] board) {
String row = "row";
String col = "col";
String block = "block";
Map<String, Integer> sudokuCharsMaps = new HashMap<String, Integer>();
for (int i = 0; i < 9; i++) {
char[] arr2 = board[i];
for (int j = 0; j < 9; j++) {
String temp = String.valueOf(arr2[j]);
if (!temp.equals(".")) {
if ((i >= 0 && i <= 2) && (j >= 0 && j <= 2)) {
if (null != sudokuCharsMaps.get(block + "33" + temp)) {
return false;
} else {
sudokuCharsMaps.put(block + "33" + temp, 1);
}
}
if ((i >= 0 && i <= 2) && (j >= 3 && j <= 5)) {
if (null != sudokuCharsMaps.get(block + "36" + temp)) {
return false;
} else {
sudokuCharsMaps.put(block + "36" + temp, 1);
}
}
if ((i >= 0 && i <= 2) && (j >= 6 && j <= 8)) {
if (null != sudokuCharsMaps.get(block + "39" + temp)) {
return false;
} else {
sudokuCharsMaps.put(block + "39" + temp, 1);
}
}
if ((i >= 3 && i <= 5) && (j >= 0 && j <= 2)) {
if (null != sudokuCharsMaps.get(block + "63" + temp)) {
return false;
} else {
sudokuCharsMaps.put(block + "63" + temp, 1);
}
}
if ((i >= 3 && i <= 5) && (j >= 3 && j <= 5)) {
if (null != sudokuCharsMaps.get(block + "66" + temp)) {
return false;
} else {
sudokuCharsMaps.put(block + "66" + temp, 1);
}
}
if ((i >= 3 && i <= 5) && (j >= 6 && j <= 8)) {
if (null != sudokuCharsMaps.get(block + "69" + temp)) {
return false;
} else {
sudokuCharsMaps.put(block + "69" + temp, 1);
}
}
if ((i >= 6 && i <= 8) && (j >= 0 && j <= 2)) {
if (null != sudokuCharsMaps.get(block + "93" + temp)) {
return false;
} else {
sudokuCharsMaps.put(block + "93" + temp, 1);
}
}
if ((i >= 6 && i <= 8) && (j >= 3 && j <= 5)) {
if (null != sudokuCharsMaps.get(block + "96" + temp)) {
return false;
} else {
sudokuCharsMaps.put(block + "96" + temp, 1);
}
}
if ((i >= 6 && i <= 8) && (j >= 6 && j <= 8)) {
if (null != sudokuCharsMaps.get(block + "99" + temp)) {
return false;
} else {
sudokuCharsMaps.put(block + "99" + temp, 1);
}
}
if (null != sudokuCharsMaps.get(row + i + temp) || null != sudokuCharsMaps.get(col + j + temp)) {
return false;
} else {
sudokuCharsMaps.put(row + i + temp, 1);
sudokuCharsMaps.put(col + j + temp, 1);
}
}
}
}
return true;
}
不使用Map來實(shí)現(xiàn)的算法:
public static boolean isValidSudoku2(char[][] board) {
// 記錄某行望抽,某位數(shù)字是否已經(jīng)被擺放
boolean[][] row = new boolean[9][10];
// 記錄某列加矛,某位數(shù)字是否已經(jīng)被擺放
boolean[][] col = new boolean[9][10];
// 記錄某 3x3 宮格內(nèi),某位數(shù)字是否已經(jīng)被擺放
boolean[][] block = new boolean[9][10];
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if (board[i][j] != '.') {
int num = board[i][j] - '0';
if (row[i][num] || col[j][num] || block[i / 3 * 3 + j / 3][num]) {
return false;
} else {
row[i][num] = true;
col[j][num] = true;
block[i / 3 * 3 + j / 3][num] = true;
}
}
}
}
return true;
}
上面這個算法有兩個比較巧妙的地方煤篙,第一個是 int num = board[i][j] - '0'; 這行代碼是字符相減(減 0)斟览,還是等于原字符;第二個地方是 block[i / 3 * 3 + j / 3][num] 辑奈,這里 i / 3 * 3 + j / 3 要注意苛茂,i / 3 * 3 的值并不等于 i R芽尽!
我剛開始的算法收到這個啟發(fā)味悄,可以簡化為:
public static boolean isValidSudoku3(char[][] board) {
String row = "row";
String col = "col";
String block = "block";
Map<String, Integer> sudokuCharsMaps = new HashMap<String, Integer>();
for (int i = 0; i < 9; i++) {
char[] arr2 = board[i];
for (int j = 0; j < 9; j++) {
String temp = String.valueOf(arr2[j]);
if (!temp.equals(".")) {
if (null != sudokuCharsMaps.get(row + i + temp) || null != sudokuCharsMaps.get(col + j + temp)
|| null != sudokuCharsMaps.get(block + i / 3 * 3 + j / 3 + temp)) {
return false;
} else {
sudokuCharsMaps.put(row + i + temp, 1);
sudokuCharsMaps.put(col + j + temp, 1);
sudokuCharsMaps.put(block + i / 3 * 3 + j / 3 + temp, 1);
}
}
}
}
return true;
}