Java 4ms solution


  • 0
    F
    public boolean isValidSudoku(char[][] board) {
        for(int i=0;i<9;i++){
            int[] set1=new int[10];
            int[] set2=new int[10];
            int[] set3=new int[10];
            for(int j=0;j<9;j++){
                //System.out.println(i+","+j);
                
                char v=board[i][j];
                //System.out.print(v+" ");
                if(v != '.'){
                    if(set1[v-'0'] == 1)
                        return false;
                    set1[v-'0']=1;
                }
                v=board[j][i];
                //System.out.print(v+" ");
                if(v != '.'){
                    if(set2[v-'0'] == 1)
                        return false;
                    set2[v-'0']=1;
                }
                v=board[3*(i%3)+j/3][3*(i/3)+j%3];
                //System.out.print(v+"\n");
                if(v != '.'){
                    if(set3[v-'0']==1)
                        return false;
                    set3[v-'0']=1;
                }
            }
        }
        return true;
     
    }

Log in to reply
 

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