Modularized Java Solution beats 70%!!!


  • -1
    A
    public class Solution {
        public boolean isValidSudoku(char[][] board) {
            if (board == null || board.length == 0){
                return false;
            }
            for (int i = 0; i < board.length; i++){
                boolean validRow = validateRow (i, board);
                if (validRow == false){
                    return false;
                }
            }
            for (int j = 0; j < board[0].length; j++){
                boolean validColumn = validateColumn (j, board);
                if (validColumn == false){
                    return false;
                }
            }
            
            for (int i = 0; i < board.length; i+=3){
                for (int j = 0; j < board[0].length; j+=3){
                    boolean validCell = validateCell(i,j,board);
                    if (validCell == false){
                        return false;
                    }
                }
            }
            return true;
        }
        
        public boolean validateRow(int row,char[][] board){
            boolean[] validateArr = new boolean[board.length];
            int j = 0;
            while(j < board.length){
                if (board[row][j] == '.'){
                    j++;
                    continue;
                }
                else if (validateArr[board[row][j] - '0' - 1] == false){
                    validateArr[board[row][j] - '0' - 1] = true;
                    j++;
                }
                else {
                    return false;
                }
            }
            return true;
        }
        
        public boolean validateColumn(int column,char[][] board){
            boolean[] validateArr = new boolean[board[0].length];
            int j = 0;
            while(j < board[0].length){
                if (board[j][column] == '.'){
                    j++;
                    continue;
                }
                else if (validateArr[board[j][column] - '0' - 1] == false){
                    validateArr[board[j][column] - '0' - 1] = true;
                    j++;
                }
                else {
                    return false;
                }
            }
            return true;
        }
        
        public boolean validateCell (int row,int column, char[][] board){
            boolean[] validateArr = new boolean[board[0].length];
            for (int i = row; i < row+3; i++){
                for (int j = column; j < column+3; j++){
                    if (board[i][j] == '.'){
                        continue;
                    }
                    else if (validateArr[board[i][j] - '0' - 1] == false){
                        validateArr[board[i][j] - '0' - 1] = true;
                    }
                    else {
                        return false;
                    }
                }
            }
            return true;
        }
    }

Log in to reply
 

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