[LeetCode]题解(python):036-Valid Sudoku
题目来源
https://leetcode.com/problems/valid-sudoku/
etermine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
. A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
题意分析
Input: a list means the sudoku with each element in the list is a str
Output:True or False
Conditions:看已有的数字是否满足数独的条件(每行、列、小九方格不能一样的数字)
题目思路
此题是判断已有的矩阵块是否为有效的数独组,分别遍历每一行,每一列,每一个小九方格即可,注意下标范围
AC代码(Python)
_author_ = "YE"
# -*- coding:utf-8 -*-
class Solution(object):
def verifyRow(self, board):
for i in range(9):
L = []
for j in range(9):
if board[i][j] == '.':
continue
elif board[i][j] in L:
return False
else:
L.append(board[i][j])
return True def verifyColumn(self, board):
for j in range(9):
L = []
for i in range(9):
if board[i][j] == '.':
continue
elif board[i][j] in L:
return False
else:
L.append(board[i][j])
return True def verifySquare(self, board):
for i in range(3):
for j in range(3):
L = []
for k in range(3):
for x in range(3):
if board[3 * i + k][3 * j + x] == '.':
continue
elif board[3 * i + k][3 * j + x] in L:
return False
else:
L.append(board[3 * i + k][3 * j + x])
return True def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
numbers = len(board[0])
print(numbers) # print('row:',self.verifyRow(board))
if not self.verifyRow(board):
return False
# print('column:',self.verifyColumn(board))
if not self.verifyColumn(board):
return False
# print('square:',self.verifySquare(board))
if not self.verifySquare(board):
return False return True #test code
s = Solution()
board = [".87654321","2........","3........","4........","5........","6........","7........","8........","9........"]
print(s.isValidSudoku(board))
[LeetCode]题解(python):036-Valid Sudoku的更多相关文章
- [LeetCode] 036. Valid Sudoku (Easy) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...
- [Leetcode][Python]36: Valid Sudoku
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 36: Valid Sudokuhttps://oj.leetcode.com ...
- 【LeetCode】036. Valid Sudoku
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- LeetCode 036 Valid Sudoku
题目要求:Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudo ...
- leetcode第35题--Valid Sudoku
题目:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- LeetCode(36)Valid Sudoku
题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- Java for LeetCode 036 Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...
- 036 Valid Sudoku 有效的数独
详见:https://leetcode.com/problems/valid-sudoku/description/ class Solution { public: bool isValidSudo ...
- 【LeetCode】36. Valid Sudoku 解题报告(Python)
[LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...
随机推荐
- BZOJ2610 : [Poi2003]Monkeys
考虑离线,将删边操作倒过来变成加边,等价于询问每个点什么时候与1连通 使用并查集维护,每次合并时如果有一边是1所在连通块,就把另一边的所有点的答案更新 #include<cstdio> # ...
- BZOJ3827 : [Poi2014]Around the world
把环倍长,破环成链 设f[i]表示i一次性能飞达的最右边的点,因为f[]单调递增,所以可以$O(n)$求出 这样就形成了一个树结构,对于每个节点,在其到根节点路径上二分出深度最大的点,使得其飞过一圈 ...
- linux tar 备份命令
转载:http://www.douban.com/note/57861194/ tar [-cxtzjvfpPN] 文件与目录 ….参数:-c :建立一个压缩文件的参数指令(create 的意思):- ...
- Redis错误配置详解
在使用Redis做缓存时,应用往往能得到非常高的性能.然而,如果配置不当,你将遇到很多令人头疼的问题,比如复制缓冲区限制.复制超时等. Redis提供了许多提高和维护高效内存数据库使用的工具.在无需额 ...
- 微课程--Android--Activity组建与Intent
安卓的四大基本组件 Activity是经常使用的组件 1 展示用户界面 2 响应用户操作 Service 1 在后台长时间运行 2 没有用户界面 ContentProvider 1 管理和共享应用数据 ...
- java网络编程之UDP实例
package Socket; import java.net.DatagramPacket; import java.net.InetAddress; public class Dgram { pu ...
- XSS挑战第一期Writeup
0x00 起因 这期 XSS 挑战的起因是在阅读“Modern Web Application Firewalls Fingerprinting and Bypassing XSS Filters”过 ...
- 使用CSS3实现百叶窗
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title&g ...
- CSS与JavaScript文件的位置
1.CSS 尽量放置在head标签中. 原因: 避免浏览器重新渲染: 避免阻塞JS文件的执行. 注:CSS选择器的解释顺序是 从右向左 的,所以尽量减少选择器的层级. 2.JS 尽量放置在</b ...
- mysql join表连接
1.表连接,就是将两个表合并起来,被合并的表的记录要通过中间字段,一一匹配起来左边的表的记录,形成一张临时的合并的表,并且每条记录的值都是两张表一一准确对应的 实例 尝试以下实例: root@host ...