yan chuanli 2 years ago
parent
commit
8d783d3c02
3 changed files with 51 additions and 2 deletions
  1. 18 0
      draft.py
  2. 8 2
      is_valid.py
  3. 25 0
      merge_two_lists.py

+ 18 - 0
draft.py

@@ -0,0 +1,18 @@
+from Cython.Compiler.ExprNodes import ListNode
+class Node:
+    def __init__(self,cargo = None, next = None):
+        self.cargo = cargo
+        self.next = next
+    def __str__(self):
+        #测试基本功能,输出字符串
+        return str(self.cargo)
+
+def printBackward(lists):
+    if lists == None:
+        return
+    head = lists
+    tail= lists.next
+    print(head,tail)
+    printBackward(tail)
+    print(head,tail)
+print(printBackward([123]))

+ 8 - 2
is_valid.py

@@ -14,14 +14,16 @@ def isValid(s: str):
     list = []
     for i in s:
         if i in dic.keys():
+            # 入栈
             list.append(i)
         else:
-            # if len(list) ==0 or i != dic.get(list[-1]):
+            # 出栈
             if len(list) == 0 or i != dic.get(list[-1]):
                 list.append(i)
             else:
                 list.pop()
 
+    # 栈为空。意为括号都匹配结束
     if len(list) == 0:
         return True
     else:
@@ -31,7 +33,11 @@ def isValid(s: str):
 @pytest.mark.parametrize(
     "s, expect",
     [
-        ("()[]", True)
+        # ("()[]", True),
+        # ("(({[])}", False),
+        # ("(]", False),
+        # ("([{}])", True),
+        ("21[o{00}j]", False)
     ]
 )
 def test_cases(s, expect):

+ 25 - 0
merge_two_lists.py

@@ -0,0 +1,25 @@
+from typing import Optional
+# from Cython.Compiler.ExprNodes import ListNode
+
+class ListNode:
+    def __init__(self, val=0, next=None):
+        self.val = val
+        self.next = next
+
+class Solution:
+    def mergeTwoLists(self, list1: Optional[ListNode], list2: Optional[ListNode]) -> Optional[ListNode]:
+        newList = ListNode()
+        pre = newList
+        if list1 and list2:
+            if list1.val <= list2.val:
+                pre.next = list1
+                list1 = list1.next
+            else:
+                pre.next = list2
+                list2 = list2.next
+
+            pre = pre.next
+
+            pre.next = list1 if list1 else list2
+
+            return newList