Skip to content

Commit

Permalink
添加 0037.解数独 python3版本
Browse files Browse the repository at this point in the history
添加 0037.解数独 python3版本
  • Loading branch information
jojoo15 authored Jun 3, 2021
1 parent ff2ec86 commit 3695783
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion problems/0037.解数独.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,39 @@ class Solution {
```

Python:

```python3
class Solution:
def solveSudoku(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
def backtrack(board):
for i in range(len(board)): #遍历行
for j in range(len(board[0])): #遍历列
if board[i][j] != ".": continue
for k in range(1,10): #(i, j) 这个位置放k是否合适
if isValid(i,j,k,board):
board[i][j] = str(k) #放置k
if backtrack(board): return True #如果找到合适一组立刻返回
board[i][j] = "." #回溯,撤销k
return False #9个数都试完了,都不行,那么就返回false
return True #遍历完没有返回false,说明找到了合适棋盘位置了
def isValid(row,col,val,board):
for i in range(9): #判断行里是否重复
if board[row][i] == str(val):
return False
for j in range(9): #判断列里是否重复
if board[j][col] == str(val):
return False
startRow = (row // 3) * 3
startcol = (col // 3) * 3
for i in range(startRow,startRow + 3): #判断9方格里是否重复
for j in range(startcol,startcol + 3):
if board[i][j] == str(val):
return False
return True
backtrack(board)
```

Go:

Expand Down

0 comments on commit 3695783

Please sign in to comment.