@@ -28,6 +28,18 @@ class Solution:
28
28
return
29
29
```
30
30
31
+ ``` Python
32
+ class Solution :
33
+ def reverseString (self , s : List[str ]) -> None :
34
+ """
35
+ Do not return anything, modify s in-place instead.
36
+ """
37
+ mid = len (s) // 2
38
+ for i in range (mid):
39
+ s[i], s[- i- 1 ] = s[- i- 1 ], s[i]
40
+ return s
41
+ ```
42
+
31
43
### [ swap-nodes-in-pairs] ( https://leetcode-cn.com/problems/swap-nodes-in-pairs/ )
32
44
33
45
> 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
@@ -47,6 +59,18 @@ class Solution:
47
59
return head
48
60
```
49
61
62
+ ``` Python
63
+ class Solution :
64
+ def swapPairs (self , head : ListNode) -> ListNode:
65
+ if head and head.next:
66
+ next_pair = self .swapPairs(head.next.next)
67
+ temp = head.next
68
+ head.next = next_pair
69
+ temp.next = head
70
+ head = temp
71
+ return head
72
+ ```
73
+
50
74
### [ unique-binary-search-trees-ii] ( https://leetcode-cn.com/problems/unique-binary-search-trees-ii/ )
51
75
52
76
> 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树。
@@ -76,6 +100,25 @@ class Solution:
76
100
return generateTrees_rec(1 , n) if n > 0 else []
77
101
```
78
102
103
+ ``` Python
104
+ class Solution :
105
+ def generateTrees (self , n : int ) -> List[TreeNode]:
106
+ if not n:
107
+ return []
108
+ def helper (i , j ):
109
+ if i > j:
110
+ return [None ]
111
+ result = []
112
+ for m in range (i, j+ 1 ):
113
+ left = helper(i, m- 1 )
114
+ right = helper(m+ 1 , j)
115
+ for l in left:
116
+ for r in right:
117
+ result.append(TreeNode(m, l, r))
118
+ return result
119
+ return helper(1 , n)
120
+ ```
121
+
79
122
## 递归 + 备忘录 (recursion with memorization, top-down DP)
80
123
81
124
### [ fibonacci-number] ( https://leetcode-cn.com/problems/fibonacci-number/ )
@@ -101,6 +144,17 @@ class Solution:
101
144
return fib_rec(N)
102
145
```
103
146
147
+ ``` Python
148
+ class Solution :
149
+ def fib (self , n : int ) -> int :
150
+ if not n:
151
+ return 0
152
+ first, second = 0 , 1
153
+ for i in range (2 , n+ 1 ):
154
+ first, second = second, first + second
155
+ return second
156
+ ```
157
+
104
158
## 练习
105
159
106
160
- [ ] [ reverse-string] ( https://leetcode-cn.com/problems/reverse-string/ )
0 commit comments