Skip to content

Commit 75ddc36

Browse files
committed
go -> python
1 parent 699456f commit 75ddc36

File tree

1 file changed

+65
-79
lines changed

1 file changed

+65
-79
lines changed

advanced_algorithm/recursion.md

Lines changed: 65 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -10,81 +10,70 @@
1010

1111
> 编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组  `char[]`  的形式给出。
1212
13-
```go
14-
func reverseString(s []byte) {
15-
res := make([]byte, 0)
16-
reverse(s, 0, &res)
17-
for i := 0; i < len(s); i++ {
18-
s[i] = res[i]
19-
}
20-
}
21-
func reverse(s []byte, i int, res *[]byte) {
22-
if i == len(s) {
23-
return
24-
}
25-
reverse(s, i+1, res)
26-
*res = append(*res, s[i])
27-
}
13+
```Python
14+
class Solution:
15+
def reverseString(self, s: List[str]) -> None:
16+
"""
17+
Do not return anything, modify s in-place instead.
18+
"""
19+
def rev_rec(s, i, j):
20+
if i >= j:
21+
return
22+
s[i], s[j] = s[j], s[i]
23+
rev_rec(s, i + 1, j - 1)
24+
return
25+
26+
rev_rec(s, 0, len(s) - 1)
27+
28+
return
2829
```
2930

3031
[swap-nodes-in-pairs](https://leetcode-cn.com/problems/swap-nodes-in-pairs/)
3132

3233
> 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
3334
> **你不能只是单纯的改变节点内部的值**,而是需要实际的进行节点交换。
3435
35-
```go
36-
func swapPairs(head *ListNode) *ListNode {
37-
// 思路:将链表翻转转化为一个子问题,然后通过递归方式依次解决
38-
// 先翻转两个,然后将后面的节点继续这样翻转,然后将这些翻转后的节点连接起来
39-
return helper(head)
40-
}
41-
func helper(head *ListNode)*ListNode{
42-
if head==nil||head.Next==nil{
36+
```Python
37+
class Solution:
38+
def swapPairs(self, head: ListNode) -> ListNode:
39+
40+
if head is not None and head.next is not None:
41+
head_next_pair = self.swapPairs(head.next.next)
42+
p = head.next
43+
head.next = head_next_pair
44+
p.next = head
45+
head = p
46+
4347
return head
44-
}
45-
// 保存下一阶段的头指针
46-
nextHead:=head.Next.Next
47-
// 翻转当前阶段指针
48-
next:=head.Next
49-
next.Next=head
50-
head.Next=helper(nextHead)
51-
return next
52-
}
5348
```
5449

5550
[unique-binary-search-trees-ii](https://leetcode-cn.com/problems/unique-binary-search-trees-ii/)
5651

5752
> 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树。
5853
59-
```go
60-
func generateTrees(n int) []*TreeNode {
61-
if n==0{
62-
return nil
63-
}
64-
return generate(1,n)
65-
66-
}
67-
func generate(start,end int)[]*TreeNode{
68-
if start>end{
69-
return []*TreeNode{nil}
70-
}
71-
ans:=make([]*TreeNode,0)
72-
for i:=start;i<=end;i++{
73-
// 递归生成所有左右子树
74-
lefts:=generate(start,i-1)
75-
rights:=generate(i+1,end)
76-
// 拼接左右子树后返回
77-
for j:=0;j<len(lefts);j++{
78-
for k:=0;k<len(rights);k++{
79-
root:=&TreeNode{Val:i}
80-
root.Left=lefts[j]
81-
root.Right=rights[k]
82-
ans=append(ans,root)
83-
}
84-
}
85-
}
86-
return ans
87-
}
54+
注意:此题用来训练递归思维有理论意义,但是实际上算法返回的树并不是 deep copy,多个树之间会共享子树。
55+
56+
```Python
57+
class Solution:
58+
def generateTrees(self, n: int) -> List[TreeNode]:
59+
60+
def generateTrees_rec(i, j):
61+
62+
if i > j:
63+
return [None]
64+
65+
result = []
66+
for m in range(i, j + 1):
67+
left = generateTrees_rec(i, m - 1)
68+
right = generateTrees_rec(m + 1, j)
69+
70+
for l in left:
71+
for r in right:
72+
result.append(TreeNode(m, l, r))
73+
74+
return result
75+
76+
return generateTrees_rec(1, n) if n > 0 else []
8877
```
8978

9079
## 递归+备忘录
@@ -96,24 +85,20 @@ func generate(start,end int)[]*TreeNode{
9685
> F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
9786
> 给定  N,计算  F(N)。
9887
99-
```go
100-
func fib(N int) int {
101-
return dfs(N)
102-
}
103-
var m map[int]int=make(map[int]int)
104-
func dfs(n int)int{
105-
if n < 2{
106-
return n
107-
}
108-
// 读取缓存
109-
if m[n]!=0{
110-
return m[n]
111-
}
112-
ans:=dfs(n-2)+dfs(n-1)
113-
// 缓存已经计算过的值
114-
m[n]=ans
115-
return ans
116-
}
88+
```Python
89+
class Solution:
90+
def fib(self, N: int) -> int:
91+
92+
mem = [-1] * (N + 2)
93+
94+
mem[0], mem[1] = 0, 1
95+
96+
def fib_rec(n):
97+
if mem[n] == -1:
98+
mem[n] = fib_rec(n - 1) + fib_rec(n - 2)
99+
return mem[n]
100+
101+
return fib_rec(N)
117102
```
118103

119104
## 练习
@@ -122,3 +107,4 @@ func dfs(n int)int{
122107
- [ ] [swap-nodes-in-pairs](https://leetcode-cn.com/problems/swap-nodes-in-pairs/)
123108
- [ ] [unique-binary-search-trees-ii](https://leetcode-cn.com/problems/unique-binary-search-trees-ii/)
124109
- [ ] [fibonacci-number](https://leetcode-cn.com/problems/fibonacci-number/)
110+

0 commit comments

Comments
 (0)