题目来源

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的更多相关文章

  1. [LeetCode] 036. Valid Sudoku (Easy) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 036. ...

  2. [Leetcode][Python]36: Valid Sudoku

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 36: Valid Sudokuhttps://oj.leetcode.com ...

  3. 【LeetCode】036. Valid Sudoku

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. LeetCode 036 Valid Sudoku

    题目要求:Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudo ...

  5. leetcode第35题--Valid Sudoku

    题目:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  6. LeetCode(36)Valid Sudoku

    题目 Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  7. Java for LeetCode 036 Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  8. 036 Valid Sudoku 有效的数独

    详见:https://leetcode.com/problems/valid-sudoku/description/ class Solution { public: bool isValidSudo ...

  9. 【LeetCode】36. Valid Sudoku 解题报告(Python)

    [LeetCode]36. Valid Sudoku 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址 ...

随机推荐

  1. Android 第三方

    把优酷.土豆等取出它们真是的视频播放地址:http://vparser.com/ volley 项目地址 https://github.com/smanikandan14/Volley-demo (1 ...

  2. silverlight 跨域访问 wcf

    先介绍一下我的测试项目,我用flash和silverlight一同来调用一个webservice,一个flash客户端,一个silverlight客户端,一个web项目来host flash和silv ...

  3. c# 使用GetOleDbSchemaTable获取access数据库结构

    c# 使用GetOleDbSchemaTable获取access数据库结构 ado.net可以使用GetOleDbSchemaTable方法来获取access数据库的结构,但得到的datatable的 ...

  4. git 用Gitk /usr/bin/which: no wish

    /usr/bin/which: no wish 安装yum -y install tcl 和yum -y install tk 显示所有的分支 $gitk --all 显示所有的分支 $gitk -- ...

  5. 图解classloader加载class的流程及自定义ClassLoader

    图解classloader加载class的流程及自定义ClassLoader 博客分类: JVM JavaJVM虚拟机EXTSUN /** *  转载请注明作者longdick    http://l ...

  6. Qt 程序退出时断言错误——_BLOCK_TYPE_IS_VALID(pHead->nBlockUse),由setAttribute(Qt::WA_DeleteOnClose)引起

    最近在学习QT,自己仿写了一个简单的QT绘图程序,但是在退出时总是报错,断言错误: 报错主要问题在_BLOCK_TYPE_IS_VALID(pHead->nBlockUse),是在关闭窗口时报的 ...

  7. Web服务器上可能被包含或被请求的不同脚本源代码文件

    Web服务器上可能被包含或被请求的不同脚本源代码文件的大致数量(建议值为1024~4096). ; 如果你不能确定,则设为 0 :此设定主要用于拥有数千个源文件的站点. apc.optimizatio ...

  8. Ubuntu 启动停止脚本

    /etc/init.d 目录下的开机启动脚本 1. more redis_8010 #/bin/sh #Configurations injected by install_server below. ...

  9. new NABCD

    小组名称: 天天向上 项目名称:连连看 小组成员:王森(组长).胡丽娜.林莉.张政.张金生 新NABCD模型 N(需求) 传统的连连看有许多,玩法单一,感觉没意思,用户更希望连连看游戏增加更多的与众不 ...

  10. JavaWEB前端向服务器端发送对象

    最近项目中需要做一个关于批量删除的功能,删除条件有多个,需要从页面全部传给后台服务器程序,单个的删除,可以拼接参数给url,服务器端获取参数后执行删除操作即可.但是批量删除多个,参数会很多,传递就有些 ...