12 line java code


  • 0
        public boolean isValidSudoku(char[][] board) {
            int[][] row = new int[9][9], col = new int[9][9], sqr = new int[9][9];
            for(int i = 0; i < board.length; i ++){
                for(int j = 0; j < board[0].length; j++){
                    if(board[i][j] != '.'){
                        int item = (int)(board[i][j] - '0');
                        if(row[i][item - 1] == 1 || col[j][item - 1] == 1 || sqr[i - i % 3 + j / 3][item - 1] == 1) return false;
                        row[i][item - 1] = 1;
                        col[j][item - 1] = 1;
                        sqr[i - i % 3 + j / 3][item - 1] = 1;
            }}}
            return true;
    }
    

Log in to reply
 

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