@@ -28,6 +28,18 @@ class Solution:
2828 return
2929```
3030
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+
3143### [ swap-nodes-in-pairs] ( https://leetcode-cn.com/problems/swap-nodes-in-pairs/ )
3244
3345> 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表。
@@ -47,6 +59,18 @@ class Solution:
4759 return head
4860```
4961
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+
5074### [ unique-binary-search-trees-ii] ( https://leetcode-cn.com/problems/unique-binary-search-trees-ii/ )
5175
5276> 给定一个整数 n,生成所有由 1 ... n 为节点所组成的二叉搜索树。
@@ -76,6 +100,25 @@ class Solution:
76100 return generateTrees_rec(1 , n) if n > 0 else []
77101```
78102
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+
79122## 递归 + 备忘录 (recursion with memorization, top-down DP)
80123
81124### [ fibonacci-number] ( https://leetcode-cn.com/problems/fibonacci-number/ )
@@ -101,6 +144,17 @@ class Solution:
101144 return fib_rec(N)
102145```
103146
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+
104158## 练习
105159
106160- [ ] [ reverse-string] ( https://leetcode-cn.com/problems/reverse-string/ )
0 commit comments