题目来源:36. 有效的数独
描述
请你判断一个9x9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。
数字1-9在每一行只能出现一次。 数字1-9在每一列只能出现一次。 数字1-9在每一个以粗实线分隔的3x3宫内只能出现一次。(请参考示例图) 数独部分空格内已填入了数字,空白格用'.‘表示。
注意:
一个有效的数独(部分已被填充)不一定是可解的。
只需要根据以上规则,验证已经填入的数字是否有效即可。
示例 1:
输入:
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"]]
输出:true
示例 2:
输入:
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"]]
输出:false 解释:除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
提示:
board.length == 9
board[i].length == 9
board[i][j] 是一位数字或者 ‘.’
自行解法
class Solution {
/**
* @param String[][] $board
* @return Boolean
*/
function isValidSudoku($board) {
$nineArr = [
[
$board[0][0], $board[0][1], $board[0][2],
$board[1][0], $board[1][1], $board[1][2],
$board[2][0], $board[2][1], $board[2][2]],
[
$board[3][0], $board[3][1], $board[3][2],
$board[4][0], $board[4][1], $board[4][2],
$board[5][0], $board[5][1], $board[5][2],
],
[
$board[6][0], $board[6][1], $board[6][2],
$board[7][0], $board[7][1], $board[7][2],
$board[8][0], $board[8][1], $board[8][2],
],
[
$board[0][3], $board[0][4], $board[0][5],
$board[1][3], $board[1][4], $board[1][5],
$board[2][3], $board[2][4], $board[2][5]],
[
$board[3][3], $board[3][4], $board[3][5],
$board[4][3], $board[4][4], $board[4][5],
$board[5][3], $board[5][4], $board[5][5],
],
[
$board[6][3], $board[6][4], $board[6][5],
$board[7][3], $board[7][4], $board[7][5],
$board[8][3], $board[8][4], $board[8][5],
],
[
$board[0][6], $board[0][7], $board[0][8],
$board[1][6], $board[1][7], $board[1][8],
$board[2][6], $board[2][7], $board[2][8]],
[
$board[3][6], $board[3][7], $board[3][8],
$board[4][6], $board[4][7], $board[4][8],
$board[5][6], $board[5][7], $board[5][8],
],
[
$board[6][6], $board[6][7], $board[6][8],
$board[7][6], $board[7][7], $board[7][8],
$board[8][6], $board[8][7], $board[8][8],
],
];
foreach ($nineArr as $checkNine) {
if (!$this->checkValid($checkNine)) {
return false;
}
}
$verticalArr = [];
foreach ($board as $i => $bor) {
if (!$this->checkValid($bor)) {
return false;
}
foreach ($bor as $j => $row) {
switch ($j) {
case 0:
if (!isset($verticalArr[0])) $verticalArr[0] = [];
array_push($verticalArr[0], $row);
break;
case 1:
if (!isset($verticalArr[1])) $verticalArr[1] = [];
array_push($verticalArr[1], $row);
break;
case 2:
if (!isset($verticalArr[2])) $verticalArr[2] = [];
array_push($verticalArr[2], $row);
break;
case 3:
if (!isset($verticalArr[3])) $verticalArr[3] = [];
array_push($verticalArr[3], $row);
break;
case 4:
if (!isset($verticalArr[4])) $verticalArr[4] = [];
array_push($verticalArr[4], $row);
break;
case 5:
if (!isset($verticalArr[5])) $verticalArr[5] = [];
array_push($verticalArr[5], $row);
break;
case 6:
if (!isset($verticalArr[6])) $verticalArr[6] = [];
array_push($verticalArr[6], $row);
break;
case 7:
if (!isset($verticalArr[7])) $verticalArr[7] = [];
array_push($verticalArr[7], $row);
break;
case 8:
if (!isset($verticalArr[8])) $verticalArr[8] = [];
array_push($verticalArr[8], $row);
break;
}
}
}
foreach ($verticalArr as $checkVertical) {
if (is_array($checkVertical) && !$this->checkValid($checkVertical)) {
return false;
}
}
return true;
}
function checkValid(array $data, string $unCheck = '.'): bool
{
foreach ($data as $key => $check) {
if ($check == $unCheck) {
unset($data[$key]);
}
}
if (count($data) == count(array_unique($data))) {
return true;
}
return false;
}
}