• 36. Valid Sudoku


    36. Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

    The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

    A partially filled sudoku which is valid.

    Note:
    A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.

     1 /**
     2  * @param {character[][]} board
     3  * @return {boolean}
     4  */
     5 var isValidSudoku = function(board) {
     6     
     7     function check(row,col,num){
     8         
     9         
    10         //这一行没有重复的
    11         for(var i = 0;i<9;i++){
    12             
    13             if( i!==col&&board[row][i] ==num){
    14                 return false;
    15             }            
    16         }
    17         
    18          //这一列没有重复的
    19         for(var i = 0;i <9;i++){
    20             
    21            if( i!==row&&board[i][col] == num){
    22                 return false;
    23            }
    24         }
    25         
    26         
    27         //九宫格里都没有重复的
    28         
    29         for(var i = 3 * Math.floor((col)/3);i < 3 * Math.floor((col)/3) + 3;i++){
    30             
    31               for(var j = 3 * Math.floor((row)/3);j < 3 * Math.floor((row)/3) + 3;j++){
    32             
    33                             if(i!==col&&j!==row&&board[j][i] == num){
    34                                    return false;
    35                             }
    36                 }
    37         }
    38         
    39         
    40         //其他情况
    41         return true;
    42         
    43     }
    44     
    45   
    46     
    47     for(var row = 0;row < 9; row++){
    48         for(var col = 0;col < 9;col++){
    49            
    50                     if(board[row][col]!=="."&&!check(row,col,board[row][col])){
    51                        return false;
    52                     }     
    53         }
    54     }
    55     
    56     return true;
    57     
    58 };
  • 相关阅读:
    closure
    运算符优先级
    css妙用
    BFC (块级格式化上下文)
    display:table-cell 详解
    line-height深入理解
    margin collapse
    探究 CSS 解析原理
    python入门
    spring与线程安全
  • 原文地址:https://www.cnblogs.com/huenchao/p/7711746.html
Copyright © 2020-2023  润新知