share my one pass solution, using n^2 space


  • 0
    U
      public class Solution {
    public boolean isValidSudoku(char[][] board) {
        return check(board);
    }
    boolean check(char[][]board){
        for(int i=0;i<board.length;i++){
            boolean[]filled1=new boolean[9];
            boolean[]filled2=new boolean[9];
            boolean[]filled3=new boolean[9];
            for(int j=0;j<board.length;j++){
                char c1=board[i][j],c2=board[j][i],c3=board[(i/3)*3+j/3][(i%3)*3+j%3];
                if((c1!='.'&&filled1[c1-'1'])||(c2!='.'&&filled2[c2-'1'])||(c3!='.'&&filled3[c3-'1']))
                    return false;
                if(c1!='.')
                    filled1[c1-'1']=true;
                if(c2!='.')
                    filled2[c2-'1']=true;
                if(c3!='.')
                    filled3[c3-'1']=true;
            }
        }
        return true;
    }
    

    }


Log in to reply
 

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