Skip to content

Commit

Permalink
feat: Add python code for problem 130, 131 (azl397985856#228)
Browse files Browse the repository at this point in the history
* Fix solution on problem 88 with js, fixs azl397985856#62

* Fix solution on problem 88, fixs azl397985856#62

* Python solution for problem 90

* Add python solution for problem 92

* Add python solution for problem 94

* Add python code for problem 98

* add python code for problem 102

* add python code for problem 113

* Add python solution for problem 130

* Add python code for problem 131

* Add some comments for python code of problem 131
  • Loading branch information
kant-li authored and azl397985856 committed Nov 11, 2019
1 parent ba90706 commit 20ffddf
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
45 changes: 45 additions & 0 deletions problems/130.surrounded-regions.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Surrounded regions shouldn’t be on the border, which means that any 'O' on the

## 代码

* 语言支持:JS,Python3

```js


Expand Down Expand Up @@ -104,6 +106,49 @@ var solve = function(board) {
return board;
};
```
Python Code:
```python
class Solution:
def solve(self, board: List[List[str]]) -> None:
"""
Do not return anything, modify board in-place instead.
"""
# 如果数组长或宽小于等于2,则不需要替换
if len(board) <= 2 or len(board[0]) <= 2:
return

row, col = len(board), len(board[0])

def dfs(i, j):
"""
深度优先算法,如果符合条件,替换为A并进一步测试,否则停止
"""
if i < 0 or j < 0 or i >= row or j >= col or board[i][j] != 'O':
return
board[i][j] = 'A'

dfs(i - 1, j)
dfs(i + 1, j)
dfs(i, j - 1)
dfs(i, j + 1)

# 从外围开始
for i in range(row):
dfs(i, 0)
dfs(i, col-1)

for j in range(col):
dfs(0, j)
dfs(row-1, j)

# 最后完成替换
for i in range(row):
for j in range(col):
if board[i][j] == 'O':
board[i][j] = 'X'
elif board[i][j] == 'A':
board[i][j] = 'O'
```

## 相关题目

Expand Down
24 changes: 24 additions & 0 deletions problems/131.palindrome-partitioning.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Output:

## 代码

* 语言支持:JS,Python3

```js


Expand Down Expand Up @@ -83,6 +85,28 @@ var partition = function(s) {
};


```
```python
class Solution:
def partition(self, s: str) -> List[List[str]]:
"""回溯法"""

res = []

def helper(s, tmp):
"""
如果是空字符串,说明已经处理完毕
否则逐个字符往前测试,判断是否是回文
如果是,则处理剩余字符串,并将已经得到的列表作为参数
"""
if not s:
res.append(tmp)
for i in range(1, len(s) + 1):
if s[:i] == s[:i][::-1]:
helper(s[i:], tmp + [s[:i]])

helper(s, [])
return res
```

## 相关题目
Expand Down

0 comments on commit 20ffddf

Please sign in to comment.