请你判断一个 9 x 9 的数独是否有效。只需要 根据以下规则 ,验证已经填入的数字是否有效即可。
- 数字 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 中的每行,每列即9个 3*3 格子转成一维数组,判断这些数组中是否存在重复的元素,存在则说明 board 不是有效的数独,否则则说明board 是有效的数独。
ts
function isValidSudoku(board: string[][]): boolean {
// 判断数组中是否包含重复的值
const containsDuplicate = (nums: string[],exclude:string): boolean => {
let result: boolean = false;
const valueSet = new Set<string>()
let i = nums.length - 1
while(i>-1) {
if(nums[i] !== exclude) {
if (valueSet.has(nums[i])) {
console.log(valueSet,nums)
result = true
break
}
valueSet.add(nums[i])
}
i--
}
return result
}
// 判断每行是否有重复的值
let row = 0
while(row < board.length) {
if (containsDuplicate(board[row],'.')) {
return false
}
row ++
}
// 判断每列是否有重复的值
let col = 0
while(col < board[0].length) {
const colArr: string[] = []
for(let rowIndex = 0; rowIndex < board.length; rowIndex++) {
colArr.push(board[rowIndex][col])
}
if (containsDuplicate(colArr,'.')) {
return false
}
col++
}
// 判断在 3*3 格子中是否有重复的数字
let minBoardRow = 0, minBoardCol = 0
while(minBoardRow < board.length / 3 && minBoardCol < board[0].length / 3) {
const minMatrixArr: string[] = []
let row = 0,col = 0
while(row < 3 && col < 3) {
minMatrixArr.push(board[row + minBoardRow * 3][col + minBoardCol * 3])
if (col === 2) {
row++
col = 0
} else {
col++
}
}
if (containsDuplicate(minMatrixArr,'.')) {
return false
}
// 转到下一个 3*3 格子
if (minBoardCol === board[0].length / 3 - 1) {
minBoardRow++
minBoardCol = 0
} else {
minBoardCol++
}
}
return true
};
解答二
创建一个二维数组 rows 保存第 i 行中数字 n 出现的次数,例如 rows[1][3] = 1 表示第 2 行数字 3 出现了 1 次;创建一个二维数组 columns 保存第 j 列中数字 n 出现的次数,例如 columns[1][3] = 1 表示第 2 列数字 3 出现的 1 次
9 x 9 的数独有 9 个 3 * 3 的小九宫格,创建一个三维数组 subboxes 保存第 i 行第 j 列的小九宫格中数字 n 出现的次数,例如 subboxes[1][1][3] = 1 表示第 2 列第 2 行数字 3 出现了 1 次。
9 * 9 数独的下标 i 和 j 对应小九宫格的下标为 [⌊3/i⌋][⌊3/j⌋]。
ts
function isValidSudoku(board: string[][]): boolean {
const rows: number[][] = new Array(9).fill(0).map(() => new Array(9).fill(0))
const columns: number[][] = new Array(9).fill(0).map(() => new Array(9).fill(0))
const subboxes: number[][][] = new Array(3).fill(0).map(() => new Array(3).fill(0).map(() => new Array(9).fill(0)))
for(let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
const char = board[i][j]
if (char !== '.') {
// 这里减 1 是因为数独中的数字为 1 - 9,但是数组的下标从 0 开始
const num = Number(char) - 1
rows[i][num]++
columns[j][num]++
subboxes[Math.floor(i/3)][Math.floor(j/3)][num]++
if (rows[i][num] > 1 || columns[j][num] > 1 || subboxes[Math.floor(i/3)][Math.floor(j/3)][num] > 1) {
return false
}
}
}
}
return true
};