首頁 > 軟體

前端演演算法題解leetcode36-有效的數獨範例

2022-09-26 14:05:17

題目

題目地址

請你判斷一個 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.length == 9

board[i].length == 9

board[i][j] 是一位數位(1-9)或者 '.'

解題思路-分別處理

分別處理每一行、每一列以及每一個九宮格,哪一部分驗證失敗,都返回 false,如果都驗證通過,返回 true

程式碼實現

function getOrigin(){
    return {
        '1':0,
        '2':0,
        '3':0,
        '4':0,
        '5':0,
        '6':0,
        '7':0,
        '8':0,
        '9':0,
    }
}
function checkArr(arr){
    const counts = getOrigin()
    for(let i = 0;i<9;i++){
        if(counts[arr[i]]){
            return false
        }else{
            counts[arr[i]]++
        }
    }
    return true
}
var isValidSudoku = function(board) {
    // 處理每一行
    for(let i = 0;i<9;i++){
        if(!checkArr(board[i])){
            return false
        }
    }
    // 處理每一列
    for(let i = 0;i<9;i++){
        const arr = []
        for(let j = 0;j<9;j++){
            if(board[j][i] === '.'){
                continue
            }
            arr.push(board[j][i])
        }
        if(!checkArr(arr)){
            return false
        }
    }
    // 處理 9 個九宮格
    for(let i = 0;i<3;i++){
        for(let j = 0;j<3;j++){
            const arr = []
            for(let k = j*3;k<j*3+3;k++){
                for(let h = 3*i;h<3*i+3;h++){
                    if(board[k][h] === '.'){
                        continue
                    }
                    arr.push(board[k][h])
                }
            }
            if(!checkArr(arr)){
                return false
            }
        }
    }
    return true
}

解題思路-一次掃描判斷所有

首先建立 lines 記錄每一行出現的數位的次數,columns 記錄每一列出現的數位的次數,scratchableLatexs 記錄每一個九空格出現的數位的次數。

然後雙層迴圈可以遍歷輸入陣列中的每一個元素,根據當前 i,j 值判斷屬於哪一行,哪一列以及哪一個九宮格,分別判斷即可。

程式碼實現

function getOrigin(){
    return {
        '1':0,
        '2':0,
        '3':0,
        '4':0,
        '5':0,
        '6':0,
        '7':0,
        '8':0,
        '9':0,
    }
}
var isValidSudoku = function(board) {
    const lines = []
    const columns = []
    const scratchableLatexs = []
    for(let i = 0;i<9;i++){
        lines[i] = getOrigin()
        columns[i] = getOrigin()
        scratchableLatexs[i] = getOrigin()
    }
    for(let i = 0;i<9;i++){
        for(let j = 0;j<9;j++){
            const item = board[i][j]
            if(item === '.'){
                continue
            }
            if(lines[i][item]){
                return false
            }else{
                lines[i][item]++
            }
            if(columns[j][item]){
                return false
            }else{
                columns[j][item]++
            }
            if(i<3){
                if(j<3){
                    if(scratchableLatexs[0][item]){
                        return false
                    }else{
                        scratchableLatexs[0][item]++
                    }
                }else if(j<6){
                    if(scratchableLatexs[1][item]){
                        return false
                    }else{
                        scratchableLatexs[1][item]++
                    }
                }else{
                    if(scratchableLatexs[2][item]){
                        return false
                    }else{
                        scratchableLatexs[2][item]++
                    }
                }
            }else if(i<6){
                if(j<3){
                    if(scratchableLatexs[3][item]){
                        return false
                    }else{
                        scratchableLatexs[3][item]++
                    }
                }else if(j<6){
                    if(scratchableLatexs[4][item]){
                        return false
                    }else{
                        scratchableLatexs[4][item]++
                    }
                }else{
                    if(scratchableLatexs[5][item]){
                        return false
                    }else{
                        scratchableLatexs[5][item]++
                    }
                }
            }else{
                if(j<3){
                    if(scratchableLatexs[6][item]){
                        return false
                    }else{
                        scratchableLatexs[6][item]++
                    }
                }else if(j<6){
                    if(scratchableLatexs[7][item]){
                        return false
                    }else{
                        scratchableLatexs[7][item]++
                    }
                }else{
                    if(scratchableLatexs[8][item]){
                        return false
                    }else{
                        scratchableLatexs[8][item]++
                    }
                }
            }
        }
    }
    return true
}

至此我們就完成了 leetcode-36-有效的數獨,更多關於前端演演算法題解有效的數獨的資料請關注it145.com其它相關文章!


IT145.com E-mail:sddin#qq.com