LeetCode-探索-初级-数组-有效的数独-java

有效的数独

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

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

上图是一个部分填充的有效的数独。

数独部分空格内已填入了数字,空白格用 '.' 表示。

示例 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 形式的。

我的方法:

还是比较简单的题,方法就是行检查,列检查,小正方形检查

分为三种分开的方法:

第一种:行检查,列检查,正方形检查

第二种:行列检查,正方形检查

第三种:三个代码放在一个中检查

反正并没有什么不同。(考虑到BitSet初始化的时间的话,看似后面两种比较复杂,但也是非常得不偿失的)

方便起见,我将所有的代码一起粘上

public boolean isValidSudoku(char[][] board) {
        //check rows
        BitSet bitSet = new BitSet(9);
        for (int i = 0 ; i < 9; i ++) {
            bitSet.clear();  //for each row, bitSet should be clean
            for (int j = 0 ; j < 9 ; j ++)
                if (!check(board, i, j, bitSet))
                    return false;
        }
        //check cols
        for (int col = 0 ; col < 9 ; col ++) {
            bitSet.clear();
            for (int row = 0 ; row < 9 ; row ++)
                if (!check(board, row, col, bitSet))
                    return false;
        }
        //check small square
        //totally 3 rows and 3 cols small squares
        //each small square has 3 rows and 3 cols
        /**
         * squareRow 0 --- board[0~2][]
         * squareRow 1 --- board[3~5][]
         * squareRow 2 --- board[6~8][]
         * squareRow n --- board[ 3*n+row ][]
         * ...
         * squareCol m --- board[ 3*n+row ][ 3*m+col ]
         */
        for (int squareRow = 0 ; squareRow < 3 ; squareRow ++) {
            for (int squareCol = 0 ; squareCol < 3 ; squareCol ++) {
                bitSet.clear();
                for (int row = 0 ; row < 3 ; row ++)
                    for (int col = 0 ; col < 3 ; col ++) {
                        if (!check(board, squareRow * 3 + row, 3 * squareCol + col, bitSet))
                            return false;
                    }
            }
        }
        return true;
    }

    public boolean isValidSudoku1(char[][] board) {
        //check rows and cols
        BitSet[] bitSets = new BitSet[]{
                new BitSet(9), new BitSet(9), new BitSet(9),
                new BitSet(9), new BitSet(9), new BitSet(9),
                new BitSet(9), new BitSet(9), new BitSet(9)
        };  //cols
        BitSet bitSet = new BitSet(9);  //row
        for (int i = 0 ; i < 9; i ++) {
            bitSet.clear();  //for each row, bitSet should be clean
            for (int j = 0 ; j < 9 ; j ++)
                if (board[i][j] != '.')
                    if (!check1(board, i, j, bitSet) || !check1(board, i, j, bitSets[j]))
                        return false;
        }

        //check small square
        //totally 3 rows and 3 cols small squares
        //each small square has 3 rows and 3 cols
        /**
         * squareRow 0 --- board[0~2][]
         * squareRow 1 --- board[3~5][]
         * squareRow 2 --- board[6~8][]
         * squareRow n --- board[ 3*n+row ][]
         * ...
         * squareCol m --- board[ 3*n+row ][ 3*m+col ]
         */
        for (int squareRow = 0 ; squareRow < 3 ; squareRow ++) {
            for (int squareCol = 0 ; squareCol < 3 ; squareCol ++) {
                bitSet.clear();
                for (int row = 0 ; row < 3 ; row ++)
                    for (int col = 0 ; col < 3 ; col ++) {
                        if (!check(board, squareRow * 3 + row, 3 * squareCol + col, bitSet))
                            return false;
                    }
            }
        }
        return true;
    }

    public boolean isValidSudoku10(char[][] board) {
        //check rows and cols
        BitSet[] rowBitSets = new BitSet[]{
                new BitSet(9), new BitSet(9), new BitSet(9),
                new BitSet(9), new BitSet(9), new BitSet(9),
                new BitSet(9), new BitSet(9), new BitSet(9)
        };  //cols
        BitSet bitSet = new BitSet(9);  //squares
        BitSet[] colBitSets = new BitSet[]{
                new BitSet(9), new BitSet(9), new BitSet(9),
                new BitSet(9), new BitSet(9), new BitSet(9),
                new BitSet(9), new BitSet(9), new BitSet(9)
        };


        //check small square
        //totally 3 rows and 3 cols small squares
        //each small square has 3 rows and 3 cols
        /**
         * squareRow 0 --- board[0~2][]
         * squareRow 1 --- board[3~5][]
         * squareRow 2 --- board[6~8][]
         * squareRow n --- board[ 3*n+row ][]
         * ...
         * squareCol m --- board[ 3*n+row ][ 3*m+col ]
         */
        int indexRow = 0;
        int indexCol = 0;
        for (int squareRow = 0 ; squareRow < 3 ; squareRow ++) {
            for (int squareCol = 0 ; squareCol < 3 ; squareCol ++) {
                bitSet.clear();
                for (int row = 0 ; row < 3 ; row ++)
                    for (int col = 0 ; col < 3 ; col ++) {
                        indexRow = squareRow * 3 + row;
                        indexCol = squareCol * 3 + col;
                        if (board[indexRow][indexCol] != '.')
                            if (!check1(board, indexRow, indexCol, bitSet) ||
                                !check1(board, indexRow, indexCol, rowBitSets[indexRow])||
                                !check1(board, indexRow, indexCol, colBitSets[indexCol]))
                                return false;
                    }
            }
        }
        return true;
    }

    public boolean check(char[][] board, int row, int col, BitSet bitSet) {
        if (board[row][col] != '.')
            if (bitSet.get(board[row][col]))  //if it exists
                return false;
            else
                bitSet.set(board[row][col]);
        //end if
        //end if
        return true;
    }

    public boolean check1(char[][] board, int row, int col, BitSet bitSet) {
        if (bitSet.get(board[row][col]))  //if it exists
            return false;
        else
            bitSet.set(board[row][col]);
        //end if
        //end if
        return true;
    }

官网上给的最佳方法(emmm总是要有这一步的~):

先确定行列,比较同一行之后的数;比较同一列之后的数;之后在所在的九宫格中,比较之后的列的数(虽然答案上很容易看出来是行,但是,行列是等价的)

上代码还是比较容易一点(其实画一个图更容易):

/**
     * method:
     * 1.Identify the row count and line count.
     * 2.Compare the following numbers in current line.
     * 3.Compare the following numbers in current row.
     * 4.Compare the following numbers in following lines that in current square.
     * @param board
     * @return
     */
    public boolean isValidSudoku11(char[][] board) {
        //step 1 in comment
        for (int rowCount = 0 ; rowCount < 9 ; rowCount ++)
            for (int lineCount = 0 ; lineCount < 9 ; lineCount ++) {
                if (board[rowCount][lineCount] == '.')
                    continue;
                //step 2 in comment
                for (int row = rowCount + 1 ; row < 9 ; row ++)
                    if (board[rowCount][lineCount] == board[row][lineCount])
                        return false;
                //step 3 in comment
                for (int line = lineCount + 1 ; line < 9 ; line ++)
                    if (board[rowCount][lineCount] == board[rowCount][line])
                        return false;
                //step 4 in comment
                /**
                 * For this step:
                 * If we want to select a sub array, we must know the range of row and col
                 * The range of col is obviously lineCount+1 to lineCount%3==0
                 * The range of row is obviously 3(from rowCount/3*3 to rowCount/3*3+3)
                 */
                int rowLimitation = rowCount / 3 * 3 + 3;
                for (int col = lineCount + 1 ; col % 3 != 0 ; col ++)
                    for (int row = rowCount / 3 * 3 ; row < rowLimitation ; row ++)
                        if (board[rowCount][lineCount] == board[row][col])
                            return false;
            }
            //end loop
        return true;
    }

其实挺奇怪的,我修改之后的代码也不能排在时间消耗最少的里面

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值