Share my solution in java using 3 9x9 boolean array


  • 1
    E
    public class Solution {
    public boolean isValidSudoku(char[][] board) {
        boolean[][] rows = new boolean[9][9];
        boolean[][] cols = new boolean[9][9];
        boolean[][] cell = new boolean[9][9];
        for(int i=0;i<board.length;i++){
            for(int j=0;j<board[0].length;j++){
                if(board[i][j]!='.'){
                    int num = (int)(board[i][j]-'1');
                    if(rows[i][num]){
                        return false;
                    }else{
                        rows[i][num]=true;
                    }
                    if(cols[j][num]){
                        return false;
                    }else{
                        cols[j][num]=true;
                    }
                    if(cell[(i/3)*3+j/3][num]){
                        return false;
                    }else{
                        cell[(i/3)*3+j/3][num]=true;
                    }
                }
            }
        }
        return true;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.