浏览代码

two pointer

Raychar 2 年之前
父节点
当前提交
953e482f61

二进制
.DS_Store


+ 1 - 1
.idea/leetcode.iml

@@ -2,7 +2,7 @@
 <module type="PYTHON_MODULE" version="4">
   <component name="NewModuleRootManager">
     <content url="file://$MODULE_DIR$" />
-    <orderEntry type="jdk" jdkName="Python 3.10" jdkType="Python SDK" />
+    <orderEntry type="jdk" jdkName="Python 3.8 (py38) (2)" jdkType="Python SDK" />
     <orderEntry type="sourceFolder" forTests="false" />
   </component>
 </module>

+ 1 - 1
.idea/misc.xml

@@ -1,4 +1,4 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
-  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.10" project-jdk-type="Python SDK" />
+  <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (py38) (2)" project-jdk-type="Python SDK" />
 </project>

+ 0 - 0
array/search_insert.py → dichotomy/search_insert.py


+ 55 - 0
double_pointer/has_cycle.py

@@ -0,0 +1,55 @@
+from typing import Optional
+
+import pytest
+
+from utils import ListNode, arr_to_linklist
+
+def hasCycle(head: Optional[ListNode]) -> bool:
+    # if not head or not head.next:
+    #     return False
+    # p = q = head
+    # while p.next or q.next:
+    #     if q.next is None:
+    #         p = p.next
+    #         q = p.next
+    #     elif p.next is None:
+    #         return False
+    #     elif q.next != p:
+    #         q = q.next
+    #     else:
+    #         return True
+
+    # 哈希表法
+    # res = set()
+    # while head:
+    #     if head in res:
+    #         return True
+    #     res.add(head)
+    #     head = head.next
+    # return False
+
+    # 快慢指针法
+    if not head or not head.next:
+        return False
+    slow = head
+    fast = head.next
+    while fast != slow:
+        if not fast or not fast.next:
+            return False
+        slow = slow.next
+        fast = fast.next.next
+
+    return True
+
+
+
+
+@pytest.mark.parametrize(
+    "head, expect",
+    [
+        ([3,2,0,-4], True)
+    ]
+)
+
+def test_cases(head, expect):
+    assert hasCycle(arr_to_linklist(head)) == expect

+ 33 - 0
double_pointer/is_ palindrome.py

@@ -0,0 +1,33 @@
+import pytest
+
+
+def isPalindrome(s: str):
+    s = ''.join(ch for ch in s if ch.isalnum()).lower()
+    if s == '':
+        return True
+    lens = len(s)
+    p, q = 0, lens - 1
+    while p < lens or q > 0:
+        if p >= q:
+            return True
+        elif s[p] == s[q]:
+            p += 1
+            q -= 1
+        else:
+            return False
+
+@pytest.mark.parametrize(
+    "s, expect",
+    [
+        ("", True),
+        (".", True),
+        ("aa", True),
+        ("A man, a plan, a canal: Panama", True),
+        ("a123a", False)
+    ]
+)
+
+def test_cases(s, expect):
+    assert isPalindrome(s) == expect
+
+

+ 16 - 0
double_pointer/is_happy.py

@@ -0,0 +1,16 @@
+import math
+def isHappy(n: int) -> bool:
+    if n == 1:
+        return True
+    while n != 1:
+        n = str(n)
+        a = 0
+        for i in n:
+            i = int(i)
+            a = i * i + a
+        if a < 10 and a > 1 and a != 7:
+            return False
+        n = a
+    return True
+
+print(isHappy(2))

+ 0 - 1
array/merge_ordered_arrays.py → double_pointer/merge_ordered_arrays.py

@@ -1,6 +1,5 @@
 from typing import List
 
-
 def merge(nums1: List[int], m: int, nums2: List[int], n: int) -> None:
     # 法1
     nums1[m:] = nums2

+ 0 - 0
array/remove_array_duplicates.py → double_pointer/remove_array_duplicates.py


+ 3 - 13
draft.py

@@ -1,14 +1,4 @@
-from typing import Optional
-from utils import ListNode, list_to_linklist
-
-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])))
+s = "A man, a plan, a canal: Panama".lower()
+b = ''.join(ch for ch in s if ch.isalnum()).lower()
+print(b)