[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/ 题目地址 ...
随机推荐
- BZOJ1111 : [POI2007]四进制的天平Wag
POI2007完结撒花~ 首先将n转化为四进制,从低位到高位DP f[i]表示这一位不向下一位借位 g[i]表示这一位向下一位借位,但借的那个不算在i f[0]=0,g[0]=inf f[i]=mer ...
- IP地址在数据库里面的存储方式
大多数公司的表结构都需要经过DBA进行审核,有时候你会看到存储IP地址采用varchar(15),这种方式都是传统的做法,这种方法需要占用15个字节,那么有更省空间的做法么?肯定是有的,那就是用int ...
- kail-linux 下载地址
http://archive-6.kali.org/kali-images/kali-2016.1/kali-linux-2016.1-i386.iso 选择debian 32/64bit安装 开始启 ...
- SAE saestorage.class.php文件的封装代码
Source for file saestorage.class.php Documentation is available at saestorage.class.php <?php /** ...
- ArcGISDynamicMapServiceLayer 实现条件过滤
<html> <head> <meta http-equiv="Content-Type" content="text/html; ...
- QuickStart OpenvirteX
参考:ubuntu14.04安装OpenVirteX 预准备: Java 7 sudo add-apt-repository ppa:webupd8team/java sudo apt-get upd ...
- 常用Ubuntu 命令
sudo apt-get updatesudo apt-get upgrade以下2選1sudo apt-get install ubuntu-desktop如果你只想安装必要的桌面管理软件而不想安装 ...
- ThinkPhp 中Action控制器中动态改变自动完成规则(使用setProperty)
Common.php中定义的方法 在更新数据时动态规则中 "完成时间" 必须设定为2
- xml追加节点
添加方法 public void XmlAppend(VisitM vm) { XmlDocument xmldoc = new XmlDocument(); string path = Server ...
- textView截取字符串-医生工作台1期
textfield截取字符串 ios7 会崩溃 解: 之前的写法是这样的 正确的写法: 先判断markedTextRange是否为nil, markedTextRange这个属性是啥意思呢 表 ...