[Leetcode] [medium] [36] [valid Sudoku JavaScript]

Title Description

36. Effective Sudoku

Determining a number of unique 9x9 is valid. Only according to the following rules, to verify whether the numbers already filled can be effective.

The numbers 1-9 appear only once in each row.
The numbers 1-9 appear only once in each column.
Figures 1-9 only appear once in each 3x3 intrauterine separated by a thick solid line.

The figure is the number of valid only a partially filled.

Sudoku part of this space has been filled in the numbers, empty cells with '.' Representation.

Example 1:

Input:

[
["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"]
]


Output: true


Example 2:

Input:

[
  ["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"]
]

Output: false
interpretation: In addition to the first row of the first number from 5 to 8 outside, are the same as Example 1 other digital this space.
However, due to the upper left corner of the palace has two 3x3 8 exist, so this number alone is not valid.


Description:

Only a valid number (portion has been filled) is not necessarily solvable.
According to the above rules only need to verify whether the figures have been filled can be effective.
Given the number of unique sequence comprises only numeric characters 1-9 and '.'.
Sudoku will always be given in the form of 9x9.

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/valid-sudoku
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

 

answer:

answer:

Personal thoughts:

  1, each line detection for duplicate numbers;

  2, each column detection for duplicate numbers;

  3, for duplicate numbers in each of the 3 * 3 detection.

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
    colArr the let= [] 
    The let blockArr = []
     // each row whether duplicate detection 
    for (the let I = 0; I <board.length; I ++ ) { 
        Result = isRepeat (Board [I])
         IF (! Result) { 
            the console.log ( 'is repetitively' )
             return  to false 
        } 
    } 

    // whether each column duplicate detection 
    for (the let I = 0; I <board.length; I ++ ) {
         for (the 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) { 
            the console.log ( '*. 3 repeats. 3' )
             return  to false 
        } 
    } 

    // these three detected through 
    return  to true 

};
View Code

 

Guess you like

Origin www.cnblogs.com/2463901520-sunda/p/11620245.html