yan chuanli 2 years ago
parent
commit
11b113f9ca
6 changed files with 72 additions and 13 deletions
  1. BIN
      __pycache__/utils.cpython-310.pyc
  2. 23 11
      climb_stairs.py
  3. 12 2
      draft.py
  4. 21 0
      linklist/delete_duplicates.py
  5. 0 0
      linklist/merge_two_lists.py
  6. 16 0
      utils.py

BIN
__pycache__/utils.cpython-310.pyc


+ 23 - 11
climb_stairs.py

@@ -1,14 +1,26 @@
-def climbStairs(n: int) -> int:
-    if n < 1 or n > 45:
-        return 0
-    else:
-        a = 1
-        b = 1
-        for i in range(2, n + 1):
-            a, b = b, a + b
-        return b
-
-print(climbStairs(4))
+# def climbStairs(n: int) -> int:
+#     if n < 1 or n > 45:
+#         return 0
+#     else:
+#         a = 1
+#         b = 1
+#         for i in range(2, n + 1):
+#             a, b = b, a + b
+#         return b
+
+# print(climbStairs(4))
+
+def f(n):
+    cach = {}
+    def g(n):
+        if n == 1:
+            return 1
+        if n == 2:
+            return 2
+        else:
+            if g(n - 1) in cach[n - 1]:
+                return cach[n - 1] + g(n)
+            else:
 
 
 

+ 12 - 2
draft.py

@@ -1,4 +1,14 @@
+from typing import Optional
+from utils import ListNode, list_to_linklist
 
-num = [1,2,1,4,5]
-print(list(set(num)))
+def deleteDuplicates(head: Optional[ListNode]) -> Optional[ListNode]:
+    lens = 0
+    p = head
+    while p:
+        lens += 1
+        p = p.next
+
+    return lens
+
+print(deleteDuplicates(list_to_linklist([1, 2, 3, 6, 8])))
 

+ 21 - 0
linklist/delete_duplicates.py

@@ -0,0 +1,21 @@
+from typing import Optional
+from utils import ListNode, list_to_linklist
+
+def deleteDuplicates(head: Optional[ListNode]) -> Optional[ListNode]:
+    if not head:
+        return head
+
+    p = head
+    while p.next:
+        if p.val == p.next.val:
+            p.next = p.next.next
+        else:
+            p = p.next
+
+    return head
+
+print(deleteDuplicates(list_to_linklist([1, 1, 2])))
+
+
+
+

+ 0 - 0
array/merge_two_lists.py → linklist/merge_two_lists.py


+ 16 - 0
utils.py

@@ -35,4 +35,20 @@ def list_to_linklist(arr):
         p = p.next
     return head
 
+# 计算链表长度
+def len_of_linklist(head):
+    lens = 0
+    p = head
+    while p:
+        lens += 1
+        p = p.next
+    return lens
+
+def deleteNode(node):
+    node.val = node.next.val
+    node.next = node.next.next
+
+
+
+