【LeetCode】數組初級算法-有效的數獨

題目描述

有效的數獨
判斷一個 9x9 的數獨是否有效。只須要根據如下規則,驗證已經填入的數字是否有效便可。code

數字 1-9 在每一行只能出現一次。
數字 1-9 在每一列只能出現一次。
數字 1-9 在每個以粗實線分隔的 3x3 宮內只能出現一次。ip

上圖是一個部分填充的有效的數獨。leetcode

數獨部分空格內已填入了數字,空白格用 '.' 表示。get

示例 1:io

輸入:console

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

輸出:function

true

示例 2:循環

輸入:top

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

輸出:view

false

解釋: 除了第一行的第一個數字從 5 改成 8 之外,空格內其餘數字均與 示例1 相同。

但因爲位於左上角的 3x3 宮內有兩個 8 存在, 所以這個數獨是無效的。

說明:

一個有效的數獨(部分已被填充)不必定是可解的。
只須要根據以上規則,驗證已經填入的數字是否有效便可。
給定數獨序列只包含數字 1-9 和字符 '.' 。
給定數獨永遠是 9x9 形式的。

思路

分三步判斷該數獨是否有效:
1.每行是否有效:雙重循環
2.每列是否有效:雙重循環
3.每一個3*3小格是否有效:四重循環.一共九個小格子,每一個格子的頂點元素下標分別爲:
(0,0)(0,3)(0,6)(3,0)(3,3)(3,6)(6,0)(6,3)(6,6)

用num[i]記錄數字i是否使用過,num[i]標識數字i使用過,若是重複使用,則返回false.
注意下標和(數字-1)的對應,每次用完要把num清空。

JavaScript實現

/**
 * @param {character[][]} board
 * @return {boolean}
 */
var isValidSudoku = function(board) {
    let num1 = [];
    let num2 = [];
    let num3 = [];
    for(let i = 0; i < 9; i++){
        for(let posn = 0; posn < 9; posn++){
            num1[posn] = false;
            num2[posn] = false;
        }  
        for(let j = 0; j < 9; j++){
           if(board[i][j] != '.'){
               let n = parseInt(board[i][j]);
               if(!num1[n-1]){
                  num1[n-1] = true;
                }else{
                    return false;
                }
           }
           if(board[j][i] != '.'){
               let n = parseInt(board[j][i]);
               if(!num2[n-1]){
                  num2[n-1] = true;
                }else{
                    return false;
                }
           }
            
        }
    }
    for(let i = 0; i <= 6; i+=3){
        for(let j = 0; j <= 6; j+=3){
            //console.log(i,j);
            for(let posn = 0; posn < 9; posn++){
                num3[posn] = false;
            }  
            //console.log(num3);
            for(let s1 = i; s1 < i+3; s1++){
                for(let s2 = j; s2 < j+3; s2++){
                    if(board[s1][s2] != '.'){
                    let n = parseInt(board[s1][s2]);
                    //console.log(n,num3[n]);
                    if(!num3[n-1]){
                        num3[n-1] = true;
                    }else{
                       return false;
                    }
                       
                       }
                }
            }
        }
    }
    return true;
};
相關文章
相關標籤/搜索