Python 刷Leetcode题库,顺带学英语单词(9)

简介: Python 刷Leetcode题库,顺带学英语单词(9)

Valid Sudoku


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


1. Each row must contain the digits 1-9 without repetition.


2. Each column must contain the digits 1-9 without repetition.


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


A partially filled sudoku which is valid.

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.


Note:


A Sudoku board (partially filled) could be valid but is not necessarily solvable.

Only the filled cells need to be validated according to the mentioned rules.

The given board contain only digits 1-9 and the character '.' .

The given board size is always 9x9 .


题意:给定一个数独数据列表,已知部分由数字组成以及待填的部分都由‘.’代替,要求检查数独的已知部分的数字是否有效。注意只求判断是否有效,不要求一定要有解。


我的解题思路:


第一步. 判断已知列表是否由9个子列表组成,每个子列表又都有9个元素;

第二步. 把已知列表的9行,9列,“九宫格”的9格,组建一个有27个元素的新列表;

第三步. 判断新列表中每个子列表筛掉“.”后,是否都只包含1-9且元素各不相同。


>>> val = [
["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"],
["1","2",".",".","8",".",".","7","9"]
]
>>> len(val)==9 and all([len(i)==9 for i in val])
True
>>> Tmp = val # 给临时列表添加原列表的9行
>>> [Tmp.append(i) for i in list(zip(*val))] # 转置后给临时列表再添加9行,即原列表的9列
[None, None, None, None, None, None, None, None, None]
>>> t = [[j[i:i+3] for i in range(0,9,3)] for j in val]
>>> # 分拆原列表为九宫格
>>> [Tmp.append(t[i][j]+t[i+1][j]+t[i+2][j]) for j in range(3) for i in range(0,9,3)]
[None, None, None, None, None, None, None, None, None]
>>> # 把9个“宫格”都作为子列表,再次添加进临时列表Tmp中
>>> Tmp
[['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'],
 ['1', '2', '.', '.', '8', '.', '.', '7', '9'],
 ['5', '6', '.', '8', '4', '7', '.', '.', '1'],
 ['3', '.', '9', '.', '.', '.', '6', '.', '2'],
 ['.', '.', '8', '.', '.', '.', '.', '.', '.'],
 ['.', '1', '.', '.', '8', '.', '.', '4', '.'],
 ['7', '9', '.', '6', '.', '2', '.', '1', '8'],
 ['.', '5', '.', '.', '3', '.', '.', '9', '.'],
 ['.', '.', '.', '.', '.', '.', '2', '.', '.'],
 ['.', '.', '6', '.', '.', '.', '8', '.', '7'],
 ['.', '.', '.', '3', '1', '6', '.', '5', '9'],
 ['5', '3', '.', '6', '.', '.', '.', '9', '8'],
 ['8', '.', '.', '4', '.', '.', '7', '.', '.'],
 ['.', '6', '.', '.', '.', '.', '1', '2', '.'],
 ['.', '7', '.', '1', '9', '5', '.', '.', '.'],
 ['.', '6', '.', '8', '.', '3', '.', '2', '.'],
 ['.', '.', '.', '4', '1', '9', '.', '8', '.'],
 ['.', '.', '.', '.', '.', '.', '.', '6', '.'],
 ['.', '.', '3', '.', '.', '1', '.', '.', '6'],
 ['2', '8', '.', '.', '.', '5', '.', '7', '9']
]
>>>
>>> len(Tmp)
27
>>> 



Sudoku Solver


Write a program to solve a Sudoku puzzle by filling the empty cells.


A sudoku solution must satisfy all of the following rules:


1. Each of the digits 1-9 must occur exactly once in each row.


2. Each of the digits 1-9 must occur exactly once in each column.


3. Each of the the digits 1-9 must occur exactly once in each of the 9 3x3 sub-boxes of the grid.

Empty cells are indicated by the character '.' .   [#37]


20210719221916884.png


A sudoku puzzle...

20210719221935907.png



...and its solution numbers marked in red.


Note:


The given board contain only digits 1-9 and the character '.' .

You may assume that the given Sudoku puzzle will have a single unique solution.

The given board size is always 9x9 .






目录
相关文章
|
5月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
65 6
|
5月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
130 2
|
3月前
|
JSON 数据格式 Python
Python实用记录(十四):python统计某个单词在TXT/JSON文件中出现的次数
这篇文章介绍了一个Python脚本,用于统计TXT或JSON文件中特定单词的出现次数。它包含两个函数,分别处理文本和JSON文件,并通过命令行参数接收文件路径、目标单词和文件格式。文章还提供了代码逻辑的解释和示例用法。
56 0
Python实用记录(十四):python统计某个单词在TXT/JSON文件中出现的次数
|
3月前
Leetcode(最后一个单词长度)
这篇文章介绍了两种解决LeetCode第58题的方法,即计算给定字符串中最后一个单词的长度,方法包括翻转字符串和逆向遍历统计。
23 0
|
3月前
【LeetCode 20】151.反转字符串里的单词
【LeetCode 20】151.反转字符串里的单词
27 0
|
3月前
|
小程序 IDE 开发工具
Python编写单词复习小程序
Python编写单词复习小程序
24 0
|
5月前
|
算法
LeetCode第58题最后一个单词的长度
LeetCode第58题"最后一个单词的长度"的解题方法,通过从字符串末尾向前遍历并计数非空格字符,直接得出最后一个单词的长度。
LeetCode第58题最后一个单词的长度
|
5月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
78 7
|
5月前
|
算法 Python
【Leetcode刷题Python】 LeetCode 2038. 如果相邻两个颜色均相同则删除当前颜色
本文介绍了LeetCode 2038题的解法,题目要求在一个由'A'和'B'组成的字符串中,按照特定规则轮流删除颜色片段,判断Alice是否能够获胜,并提供了Python的实现代码。
60 3
|
5月前
|
算法 Python
【Leetcode刷题Python】剑指 Offer 33. 二叉搜索树的后序遍历序列
本文提供了一种Python算法,用以判断给定整数数组是否为某二叉搜索树的后序遍历结果,通过识别根节点并递归验证左右子树的值是否满足二叉搜索树的性质。
28 3