mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #417 from EnzoSeason/leetcode-37
update 37. 解数独: 提供python3 版本,简化isValid, 提高回溯函数的可读性
This commit is contained in:
@ -321,6 +321,59 @@ class Solution:
|
|||||||
backtrack(board)
|
backtrack(board)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Python3:
|
||||||
|
|
||||||
|
```python3
|
||||||
|
class Solution:
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.board = []
|
||||||
|
|
||||||
|
def isValid(self, row: int, col: int, target: int) -> bool:
|
||||||
|
for idx in range(len(self.board)):
|
||||||
|
# 同列是否重复
|
||||||
|
if self.board[idx][col] == str(target):
|
||||||
|
return False
|
||||||
|
# 同行是否重复
|
||||||
|
if self.board[row][idx] == str(target):
|
||||||
|
return False
|
||||||
|
# 9宫格里是否重复
|
||||||
|
box_row, box_col = (row // 3) * 3 + idx // 3, (col // 3) * 3 + idx % 3
|
||||||
|
if self.board[box_row][box_col] == str(target):
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def getPlace(self) -> List[int]:
|
||||||
|
for row in range(len(self.board)):
|
||||||
|
for col in range(len(self.board)):
|
||||||
|
if self.board[row][col] == ".":
|
||||||
|
return [row, col]
|
||||||
|
return [-1, -1]
|
||||||
|
|
||||||
|
def isSolved(self) -> bool:
|
||||||
|
row, col = self.getPlace() # 找个空位置
|
||||||
|
|
||||||
|
if row == -1 and col == -1: # 没有空位置,棋盘被填满的
|
||||||
|
return True
|
||||||
|
|
||||||
|
for i in range(1, 10):
|
||||||
|
if self.isValid(row, col, i): # 检查这个空位置放i,是否合适
|
||||||
|
self.board[row][col] = str(i) # 放i
|
||||||
|
if self.isSolved(): # 合适,立刻返回, 填下一个空位置。
|
||||||
|
return True
|
||||||
|
self.board[row][col] = "." # 不合适,回溯
|
||||||
|
|
||||||
|
return False # 空位置没法解决
|
||||||
|
|
||||||
|
def solveSudoku(self, board: List[List[str]]) -> None:
|
||||||
|
"""
|
||||||
|
Do not return anything, modify board in-place instead.
|
||||||
|
"""
|
||||||
|
if board is None or len(board) == 0:
|
||||||
|
return
|
||||||
|
self.board = board
|
||||||
|
self.isSolved()
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
Javascript:
|
Javascript:
|
||||||
|
Reference in New Issue
Block a user