36. Valid Sudoku leetcode Python new season 2016
2016-01-20 11:22
218 查看
Determine 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 partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
class Solution(object):
def valid(self, x, y, tmp, board):
for i in range(9):
if board[i][y] == tmp:
return False
for j in range(9):
if board[x][j] == tmp:
return False
for i in range(3):
for j in range(3):
if board[(x / 3) * 3 + i][( y / 3) * 3 + j] == tmp:
return False
return True
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
for i in range(9):
for j in range(9):
if board[i][j] == '.':
continue
tmp = board[i][j]
board[i][j] = 'D'
if self.valid(i, j, tmp, board) == False:
return False
board[i][j] = tmp
return True
The Sudoku board could be partially filled, where empty cells are filled with the character
'.'.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
class Solution(object):
def valid(self, x, y, tmp, board):
for i in range(9):
if board[i][y] == tmp:
return False
for j in range(9):
if board[x][j] == tmp:
return False
for i in range(3):
for j in range(3):
if board[(x / 3) * 3 + i][( y / 3) * 3 + j] == tmp:
return False
return True
def isValidSudoku(self, board):
"""
:type board: List[List[str]]
:rtype: bool
"""
for i in range(9):
for j in range(9):
if board[i][j] == '.':
continue
tmp = board[i][j]
board[i][j] = 'D'
if self.valid(i, j, tmp, board) == False:
return False
board[i][j] = tmp
return True
相关文章推荐
- Python使用Beautiful Soup包编写爬虫时的一些关键点
- 转来的——python webdriver自动化测试初步印象——转来的
- Python制作爬虫抓取美女图
- Numpy——ndarray对象(1):创建数组
- 35. Search Insert Position leetcode Python 2016 new season
- 编写Python爬虫抓取豆瓣电影TOP100及用户头像的方法
- 自学Python十 爬虫实战三(美女福利续)
- 34. Search for a Range leetcode Python 2016 new season
- 转来的--轻松自动化---selenium-webdriver(python) (七)---定位iframe——转来的
- 以视频爬取实例讲解Python爬虫神器Beautiful Soup用法
- python 装饰器之示例讲解
- Python算法题----1到100求和的3种加法
- Python算法题----最大公约数
- pickle-python数据持久存储
- Windows环境下Python v2.7.9,Notepad++编写程序中涉及中文,执行时CMD中显示乱码的解决办法
- 使用Python的urllib和urllib2模块制作爬虫的实例教程
- Python算法题----孙悟空吃蟠桃
- python三大神器之一fabric使用
- python (3)简单语法:字符串(strip函数),数据类型
- Python自动化面试必备 之 你真明白装饰器么?