No.36 有效的数独

题目大意

判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。

1.数字 1-9 在每一行只能出现一次。
2.数字 1-9 在每一列只能出现一次。
3.数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。

image.png

示例1

输入:
[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
输出: true

示例2

输入:
[
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]
输出: false
解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。
但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。

说明
一个有效的数独(部分已被填充)不一定是可解的。
只需要根据以上规则,验证已经填入的数字是否有效即可。
给定数独序列只包含数字 1-9 和字符 '.' 。
给定数独永远是 9x9 形式的。

分析

可以将问题分解为三个步骤:判断每行是否合法,判断每列是否合法,判断每个子数独是否合法。三次遍历解决。

代码一

public boolean isValidSudoku(char[][] board) {
        if(board == null || board.length ==0) return false;
        boolean res = true;

        //1.判断每行是否合法
        for(int i = 0;i<9 && res;i++)
            res = rowValid(board,i);  //第i行
        if(res==false) return res;
        
        //2.判断每列是否合法
        for(int i=0;i<9 && res;i++){
            //System.out.println("col="+i);
            res = colValid(board,i);   //第i列
        }
        if(res==false) return res;

        //3.判断每个小正方形
        for(int i=0;i<=6 && res;i+=3) {
            for (int j=0;j<=6 && res;j+=3) {
                res = squareValid(board,i,j);
               // if(res == false) return false;
            }
        }
        return res;
    } 
    private boolean rowValid(char[][] board,int row) {
        //判断第row行是否合法
        int[] visit = new int[10];
        for(int j=0;j<9;j++) {
            if(board[row][j] == '.') continue;
            int n = board[row][j]-'0';
            if(visit[n]!=0) return false;
            visit[n] = 1;
        }
        return true;
    }

    private boolean colValid(char[][] board,int col) {
        int[] visit = new int[10];
        for(int i = 0;i<9;i++) {
            if(board[i][col] == '.') continue;

            int n = board[i][col] - '0';
            //System.out.print(n+" ");
            if(visit[n]!=0) return false;
            visit[n] = 1;
        }
        return true;
    }

    private boolean squareValid(char[][] board, int row, int col) {
        //row,col为左顶点位置
        int[] visit = new int[10];
        for(int i=row;i

运行时间2ms,击败95.32%。

代码二:优化

其实三次遍历可合并成一个。每个子数独的计算为index = row/3 * 3 + col/3.

public boolean isValidSudoku(char[][] board) {
        int[][] row = new int[9][9];
        int[][] col = new int[9][9];
        int[][] block = new int[9][9];
        for(int i=0;i<9;i++) {
            for(int j=0;j<9;j++) {
                if(board[i][j] != '.') {
                    int n = board[i][j]-'1';
                    int blockindex = i/3*3+j/3;
                    if(row[i][n] !=0|| col[j][n]!=0 || block[blockindex][n]!=0) return false;
                    else {
                        row[i][n] = 1;
                        col[j][n] = 1;
                        block[blockindex][n] = 1;
                    }
                }
            }
        }
        return true;
    }

运行时间2ms,击败95.32%.

你可能感兴趣的:(No.36 有效的数独)