[6] 有效的数独

内容来自leetcode

1.有效的数独

题目要求

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

数字 1-9 在每一行只能出现一次。
数字 1-9 在每一列只能出现一次。
数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。(请参考示例图)
 

注意:

一个有效的数独(部分已被填充)不一定是可解的。
只需要根据以上规则,验证已经填入的数字是否有效即可。
空白格用 '.' 表示。

输入:board = 
[["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

思路

判断的条件很简单,其实就是行、列和九宫格进行判断。起初考虑到python写多维数组比较麻烦,就打算分别用行列循环和列行循环来分别对列和行进行判断,当然也确实是可行的。但是到了九宫格判断时一时间没想出来如何遍历,看了官方给出的解答是用三维数组来实现的。那也就只能先用三维数组来完成,但是程序写完之后无法通过,有的九宫格无法正确判断,检查后发现是列表的声明问题。开始是这样声明的:

subbox = [[[0]*9]*3]*3

这样确实能够生成一个表示三维数组的列表,但是问题在于子列表间并非独立存在,导致判断出现问题。

 学习了一下后发现这样声明就没问题了

subbox = [[[0 for i in range(9)]for i in range(3)]for i in range(3)]
class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        #check_row = [0] * 9
        #行判断
        for line in board:
            check_line = [0] * 9
            for cha in line:
                if cha != '.':
                    check_line[int(cha)-1] += 1
                    if check_line[int(cha)-1] > 1:
                        return False

        #列判断&九宫格
        
        subbox = [[[0 for i in range(9)]for i in range(3)]for i in range(3)]
        for j in range(0,9):
            check_row = [0] * 9
            for i in range(0,9):
                #check_row = [0] * 9
                if board[i][j] != '.':
                    check_row[int(board[i][j])-1] += 1
                    if check_row[int(board[i][j])-1] > 1:
                        return False
                    subbox[i//3][j//3][int(board[i][j])-1] +=1
                    if subbox[i//3][j//3][int(board[i][j])-1] >1:
                        return False   
        return True

在掌握了如何构建列表来表示多维数组后,可以将程序简化。

class Solution:
    def isValidSudoku(self, board: List[List[str]]) -> bool:
        check_line = [[0 for i in range(9)]for i in range(9)]
        check_row = [[0 for i in range(9)]for i in range(9)]
        subbox = [[[0 for i in range(9)]for i in range(3)]for i in range(3)]
        for j in range(0,9):
            for i in range(0,9):
                if board[i][j] != '.':
                    check_row[i][int(board[i][j])-1] +=1
                    subbox[i//3][j//3][int(board[i][j])-1] +=1
                    check_line[j][int(board[i][j])-1] +=1
                    if  check_row[i][int(board[i][j])-1] > 1 or subbox[i//3][j//3][int(board[i][j])-1] >1 or check_line[j][int(board[i][j])-1] > 1:
                        return False
        return True

本来以为第一种的内存消耗小但时间长,第二种内存消耗大但是时间短。实际运行来看其实差不多。

有大哥用位运算进行了实现,C++版本:

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        vector<int> 
        rowValidation = vector<int>(9, 0),
        colValidation = vector<int>(9, 0),
        blockValidation = vector<int>(9, 0);
        for (int i = 0; i < 9; ++i) {
            for (int j = 0; j < 9; ++j) {
                if (board[i][j] != '.') {
                    int x = (1 << (board[i][j] - '1'));
                    // 行校验
                    if (rowValidation[i] & x) {
                        return false;
                    }
                    else {
                        rowValidation[i] |= x;
                    }

                    // 列校验
                    if (colValidation[j] & x) {
                        return false;
                    }
                    else {
                        colValidation[j] |= x;
                    }

                    // 宫内校验
                    if (blockValidation[j/3*3+i/3] & x) {
                        return false;
                    }
                    else {
                        blockValidation[j/3*3+i/3] |= x;
                    }
                }
            }
        }
        return true;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值