博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
36. Valid Sudoku
阅读量:7075 次
发布时间:2019-06-28

本文共 1779 字,大约阅读时间需要 5 分钟。

36. Valid Sudoku

Determine if a Sudoku is valid, according to: .

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 };

 

转载于:https://www.cnblogs.com/huenchao/p/7711746.html

你可能感兴趣的文章