36. Valid Sudoku

博客围绕数独有效性判定展开,给出题目链接,描述判定规则,即每行、每列及每个3x3子网格中1 - 9数字无重复。还展示了三种代码,分别是效率较低的三次for循环代码、可读性好的代码,以及用到位运算的巧妙代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接

https://leetcode.com/problems/valid-sudoku/discuss/

题目描述

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

Each row must contain the digits 1-9 without repetition.
Each column must contain the digits 1-9 without repetition.
Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.

The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’.

Example 1:

Input:
[
  ["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"]
]
Output: true
Example 2:

Input:
[
  ["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"]
]
Output: false
Explanation: Same as Example 1, except with the 5 in the top left corner being 
    modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.

代码一

这是自己最初写的代码,效率比较低,三次for循环,依次判定

class Solution {
    public boolean isValidSudoku(char[][] board) {
        if(board == null || board.length != 9 || board[0].length != 9) {
            throw new RuntimeException();
        }

        int[] rows = new int[10];
        int[] cols = new int[10];
        int[] squ = new int[10];
        for(int i = 0; i < 9; i++) {
            memeryset(cols);
            for(int j = 0; j < 9; j++) {
                if(board[i][j] != '.' ) {
                    int num = board[i][j] - '0';
                    if(cols[num] == 0) {
                        cols[num] = 1;
                    }else {
                        return false;
                    }
                }
            }
        }

        System.out.println("=================");
        for(int j = 0; j < 9; j++) {
            memeryset(rows);
            for(int i = 0; i < 9; i++) {
                if(board[i][j] != '.') {
                    int num = board[i][j] - '0';
                    if(rows[num] == 0) {
                        rows[num] = 1;
                    }else {
                        return false;
                    }
                } 
            }
        }
         System.out.println("++++++++++++++++++++++++++++");
        for(int i = 0; i < 3; i++) {
            for(int j = 0; j < 3; j++) {
                memeryset(squ);
                for(int m = 0; m < 3; m++) {
                    for(int n = 0; n < 3; n++) {
                        char c = board[i*3+m][j*3+n];
                        if(c != '.') {
                            int num = c - '0';
                            if(squ[num] == 0) {
                                squ[num] = 1;
                            } else {
                                return false;
                            }
                        }
                    }
                }
            }
        }

        return true;
    }

    private void memeryset(int[] nums) {
        for(int i = 0; i < nums.length; i++) {
            nums[i] = 0;
        }
    }
}

代码二

该方法的可读性比较好,思路如下:
第4行的‘2’表示为4(2)
第4列的‘2’表示为(2)4
右上角的’2’表示为0(2)2

class Solution {
    public boolean isValidSudoku(char[][] board) {
        if(board == null || board.length != 9 || board[0].length != 9) {
            throw new RuntimeException();
        }

        Set<String> seen = new HashSet<>();
        for(int i = 0; i < 9; i++) {
            for(int j = 0; j < 9; j++) {
                if(board[i][j] != '.') {
                    String b = "(" + board[i][j] + ")";
                    if(!seen.add(b + i) || !seen.add( j + b) || !seen.add(i/3 + b + j/3) ) {
                        return false;
                    }
                }
            }
        }

        return true;
    }
}

代码三:
该方法用到了位运算,比较巧妙,也比较好懂,在以后的解题过程中,如果用0和1进行判断时,要想到使用位运算,会节省时间和空间。

class Solution {
    public boolean isValidSudoku(char[][] board) {
        if(board == null || board.length != 9 || board[0].length != 9) {
            throw new RuntimeException();
        }

        int[] rows = new int[9];
        int[] cols = new int[9];
        int[] squ = new int[9];

        for(int i = 0; i < 9; i++) {
            for(int j = 0; j < 9; j++) {
                if(board[i][j] != '.') {
                    int num = board[i][j] - '1';
                    int value = ( 1 << num);
                    if((value & rows[i]) != 0 ) {
                        return false;
                    }
                    if((value & cols[j]) != 0) {
                        return false;
                    }
                    if((value & squ[(i/3)*3 + j/3]) != 0) {
                        return false;
                    }
                     rows[i] |= value;
                    cols[j] |= value;
                    squ[(i/3)*3 + j/3] |= value;
                }

            }
        }
        return true;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值