Leetcode problem solution-effective Sudoku

Article Directory

Code

/**
 * @param {character[][]} board
 * @return {boolean}
 */
var isValidSudoku = function(board) {
    
    
    let row=[{
    
    },{
    
    },{
    
    },{
    
    },{
    
    },{
    
    },{
    
    },{
    
    },{
    
    }];
    let column=[{
    
    },{
    
    },{
    
    },{
    
    },{
    
    },{
    
    },{
    
    },{
    
    },{
    
    }];
    let box = [{
    
    },{
    
    },{
    
    },{
    
    },{
    
    },{
    
    },{
    
    },{
    
    },{
    
    }];
    for(let i=0;i<9;i++){
    
    
        for(let j=0;j<9;j++){
    
    
            let item = board[i][j];
            let boxIndex = parseInt(i/3) * 3 + parseInt(j/3);
            if(item!='.'){
    
    
                if(row[i][item]||column[j][item]||box[boxIndex][item]){
    
    
                    return false
                }else{
    
    
                    row[i][item]=1;
                    column[j][item]=1;
                    box[boxIndex][item]=1;
                }
            }
        }
    }
    return true
}

Ideas

Three rules of effective Sudoku meaning

  1. No repeated numbers in the same row
  2. No duplicate numbers in the same column
  3. There are no repeated numbers in the same small nine square grid

Use hashmap to detect duplicate data.
Each number has three identities. It belongs to a row, a column, and a certain nine-square grid.
As long as you know the location it belongs to, look
it up in the corresponding rule detection map table. If it does not exist, map it in.
If it already exists, return false

Guess you like

Origin blog.csdn.net/weixin_38616850/article/details/106392487