力扣 初级算法 全套力扣精解

初级算法-数组-有效的数独

2021-08-04  本文已影响0人  coenen
请你判断一个 9x9 的数独是否有效。只需要根据以下规则 ,验证已经填入的数字是否有效即可。

数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
数独部分空格内已填入了数字,空白格用 '.' 表示。
注意:
一个有效的数独(部分已被填充)不一定是可解的。
只需要根据以上规则,验证已经填入的数字是否有效即可。
提示:
board.length == 9
board[i].length == 9
board[i][j] 是一位数字或者 '.'


数独.png
摘一个示例做个说明.
示例 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
条件分析:
  1. 数字在行、列、3x3 宫内,只能出现一次 -> 指定区域内不能出现重复数字
  2. 根据规则判断是否有效,不考虑数学可解 -> 只考虑目前是否符合
  3. 一个9x9的矩阵 -> 9阶方阵
解决思路1:
  1. 根据分析1,定义三个存储哈希表,判断三个方面是否有重复数字
  2. 根据分析2,只要三个方向不存在重复数字即可
  3. 不考虑大量数据的情况,只有9x9 81个数据
采用双层循环,行哈希判断行,列哈希判断列,3阶方阵哈希判断三阶方阵,循环完成都不存在,则为真,一个为假则假.这里行列都好找.主要是三阶方阵存储.因为是9x9,所以可以划为9个3x3.这样对行和列求余,就能得到三阶方阵的哈希存储位置.

代码实现-Swift版本:

思路1代码:

func isValidSudoku(_ board: [[Character]]) -> Bool {
    
    var rowDict: [Int:[Character:Int]] = [:]
    var columnDict: [Int:[Character:Int]] = [:]
    var boxDict: [Int:[Character:Int]] = [:]
    for i in (0..<9) {
        for j in (0..<9) {
            let c = board[i][j]
            if c == "." {
                continue
            }
            if rowDict[i] == nil {
                rowDict[i] = [:]
            }
            if rowDict[i]![c] != nil {
                return false
            }else{
                rowDict[i]![c] = 1
            }
            
            if columnDict[j] == nil {
                columnDict[j] = [:]
            }
            if columnDict[j]![c] != nil {
                return false
            }else{
                columnDict[j]![c] = 1
            }
            
            let bIndex = i / 3 * 3 + j / 3
            if boxDict[bIndex] == nil {
                boxDict[bIndex] = [:]
            }
            if boxDict[bIndex]![c] != nil {
                return false
            }else {
                boxDict[bIndex]![c] = 1
            }
        }
    }
    
    return true
}
有效的数独 哈希表 提交结果.jpg
有效的数独 哈希表 提交结果.jpg

测试用例:

// 数独
let board: [[Character]] =
[["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"]]

// 不是数独
let board: [[Character]] =
[["5","3",".",".","7",".","3",".","."]
,["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"]]

考察要点:

上一篇下一篇

猜你喜欢

热点阅读