Java ArrayIndexOutOfBoundsException-(36.Leetcode上的有效数独)

Java ArrayIndexOutOfBoundsException-(36.Leetcode上的有效数独),java,arrays,indexoutofboundsexception,Java,Arrays,Indexoutofboundsexception,全部 我正在研究这个来自Leetcode的问题 如果有人想查看问题,请访问以下链接: 我想我几乎解决了这个问题,但是“java.lang.ArrayIndexOutOfBoundsException”有点问题,我在代码中使用大写注释来标记它,==>“if(brain[((int)board[x][y])-1])” 我检查了很多次,我认为通过执行“for(inti=0,I

全部

我正在研究这个来自Leetcode的问题

如果有人想查看问题,请访问以下链接:

我想我几乎解决了这个问题,但是“java.lang.ArrayIndexOutOfBoundsException”有点问题,我在代码中使用大写注释来标记它,==>“if(brain[((int)board[x][y])-1])”

我检查了很多次,我认为通过执行“for(inti=0,I<9;I++)”,索引应该在0~8范围内非常好,但我找不到原因

我想这一定是一个简单愚蠢的问题,但我花了很多时间来找出答案。有人能帮我吗

非常感谢帮助我的人

  public class Solution {
        public boolean isValidSudoku(char[][] board) {
            if (board.length > 9 || board[0].length > 9 || board == null) {
                return false;
            }

        boolean[] brain;
        // 9 digits are corresponding to 1 ~ 9 ==> 0 - 1, 1 - 2 ... 8 - 9
        // ex: brain[2] is checking for digit "3" , so using brain[3-1] to check "3"

        for (int x = 0; x < board.length; x++) {
            // Reset brain
            brain = new boolean[9];
            for (int y = 0; y < board[0].length; y++) {
                if (board[x][y] != '.') {


                    // THE NEXT LINE IS THE PROBLEM!!!


                    if (brain[((int) board[x][y]) - 1]) {

                        // my condition failed by using:
                        //                                                  brain[board[x][y] - 1]
                        // other's condition Partially passed by using
                        // (still failed for final submission:
                        //                                                  brain[(int) (board[x][y] - '1')]
                        // need to compare the difference


                        return false;
                    } else {
                        brain[((int) board[x][y]) - 1] = true;
                    }
                }
            }
        }


        for (int x = 0; x < board.length; x++) {
            // Reset brain
            brain = new boolean[9];
            for (int y = 0; y < board[0].length; y++) {
                if (board[x][y] != '.') {
                    if (brain[((int) board[y][x]) - 1]) {
                        return false;
                    } else {
                        brain[((int) board[y][x]) - 1] = true;
                    }
                }
            }
        }


        for (int block = 0; block < 9; block++) {
            // Reset brain
            brain = new boolean[9];
            for (int r = block / 3 * 3; r < block / 3 * 3 + 3; r++) {
                for (int c = block % 3 * 3; c < block % 3 * 3 + 3; c++) {
                    if (board[r][c] != '.') {
                        if (brain[((int) board[r][c]) - 1]) {
                            return false;
                        } else {
                            brain[((int) board[r][c]) - 1] = true;
                        }
                    }
                }
            }
        }
        return true;
    }
}
公共类解决方案{
公共布尔值isValidSudoku(字符[][]板){
如果(board.length>9 | | board[0]。length>9 | | board==null){
返回false;
}
布尔[]脑;
//9位数字对应于1~9==>0-1、1-2…8-9
//例:大脑[2]正在检查数字“3”,因此使用大脑[3-1]检查数字“3”
用于(int x=0;x