OJ Runtime Error although in my compiler all is fine


  • -1
    A

    Hello everyone! Could you please help me with this problem:
    this code getting a Runtime error although in my compiler it is working good.
    My first thought was that it is because I was using "Character.getNumericValue" function, but this was the wrong assumption. Any ideas? Thank you in advance.
    I already tried to apply static modifiers, after that I tried to exclude any static functions from any classes (like Character).
    In my own IDE it works FINE in any way...
    (the problem name is "Valid Sudoku")

    class Solution {
            public int size=9;
            public boolean isValidInRow(char[] row){
                boolean[] digits=new boolean[size];
                for (int i=0; i<row.length; ++i){
                    //int v=Character.getNumericValue(row[i]);
                    int v=row[i]-'0';
                    if (v==-1 || v==-2) continue;
                    //if (v==-2) continue;
                    if (digits[v]){
                        return false;
                    } else
                        digits[v]=true;
        
                }
                return true;
            }
        
            public boolean isValidInColumn(char[][] array, int ncol){
                char[] col=new char[size];
                for (int i=0; i<size;++i ){
                    col[i]=array[i][ncol];
                }
                return isValidInRow(col);
            }
        
            public boolean isValidInSquare(char[][] array, int x, int y){
                char[] a=new char[size];
                for (int i=0; i<3;++i)
                    for (int j=0; j<3; ++j)
                        a[3*i+j]=array[x+i][y+j];
                return isValidInRow(a);
            }
        
        
        
            public boolean isValidSudoku(char[][] board) {
                for (int i=0; i<size;++i){
                    boolean b=isValidInRow(board[i]) && isValidInColumn(board,i);
                    if (!b) return false;
                }
                for (int i=0; i<size; i+=3){
                    for (int j=0; j<size; j+=3){
                        boolean b=isValidInSquare(board, i, j);
                        if (!b) return false;
                    }
        
                }
        
                return true;
            }
        }

  • 0

    OJ now will output runtime error message. Your code has an ArrayIndexOutOfBoundsException. You could try debugging it yourself.


  • 0
    A

    Thank you very much!
    Please take my donation :)


Log in to reply
 

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