Leetcode中等36. 有效的数独JavaScript

Posted 2463901520-sunda

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode中等36. 有效的数独JavaScript相关的知识,希望对你有一定的参考价值。

题目描述

36. 有效的数独

判断一个 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 存在, 因此这个数独是无效的。


说明:

一个有效的数独(部分已被填充)不一定是可解的。
只需要根据以上规则,验证已经填入的数字是否有效即可。
给定数独序列只包含数字 1-9 和字符 ‘.‘ 。
给定数独永远是 9x9 形式的。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/valid-sudoku
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

 

解答:

解答:

个人思路:

  1、检测每行是否有重复数字;

  2、检测每列是否有重复数字;

  3、检测每个3*3内是否有重复数字。

96ms

 

技术图片
/**
 * @param character[][] board
 * @return boolean
 */
function isRepeat(arr) 
    let hash = 
    for (let i = 0; i < arr.length; i++) 
        if (!isNaN(arr[i])) 
            if (hash[arr[i]]) 
                return false
             else 
                hash[arr[i]] = 1
            
        

    
    return true

var isValidSudoku = function (board) 
    let result
    let colArr = []
    let blockArr = []
    //每一行是否重复检测
    for (let i = 0; i < board.length; i++) 
        result = isRepeat(board[i])
        if (!result) 
            console.log(‘行重复‘)
            return false
        
    

    //每一列是否重复检测
    for (let i = 0; i < board.length; i++) 
        for (let j = 0; j < board[i].length; j++) 
            if (!colArr[j]) 
                colArr[j] = []
            
            colArr[j].push(board[i][j])
        
    
    for (let i = 0; i < colArr.length; i++) 
        result = isRepeat(colArr[i])
        if (!result) 
            console.log(‘列重复‘)
            return false
        
    

    //3*3是否重复
    let block = 0
    for (let i = 0; i < board.length; i++) 
        let reset = block
        for (let j = 0; j < board[i].length; j++) 
            if (!blockArr[block]) 
                blockArr[block] = []
            
            blockArr[block].push(board[i][j])
            if ((j + 1) % 3 === 0) 
                block++
            
            if (j === 8) 
                block = reset
            
        
        if ((i + 1) % 3 === 0) 
            block += 3
        
    
    for (let i = 0; i < blockArr.length; i++) 
        result = isRepeat(blockArr[i])
        if (!result) 
            console.log(‘3*3重复‘)
            return false
        
    

    //以上三种检测都通过
    return true

;
View Code

 

以上是关于Leetcode中等36. 有效的数独JavaScript的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode刷题100天—36. 有效的数独(数组+哈希)—day26

Leetcode刷题100天—36. 有效的数独(数组+哈希)—day26

c_cpp 36.有效的数独 - 难度中等 - 2018.9.18

[JavaScript 刷题] 矩阵 - 有效的数独, leetcode 36

leetcode36. 有效的数独

leetcode36. 有效的数独