2 Commity 39cd7bf618 ... e599210496

Autor SHA1 Wiadomość Data
  yan chuanli e599210496 update 2 lat temu
  Raychar 953e482f61 two pointer 2 lat temu

BIN
.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_schoolcert) (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_schoolcert) (2)" project-jdk-type="Python SDK" />
 </project>

+ 25 - 0
array/yang_hui_triangle.py

@@ -0,0 +1,25 @@
+from typing import List
+
+
+def generate(numRows: int) -> List[List[int]]:
+    i = 1
+    res = []
+
+    while i <= numRows:
+        if i == 1:
+            res.append([1])
+            i += 1
+
+        elif i == 2:
+            res.append([1, 1])
+            i += 1
+
+        elif i > 2:
+            a = -1
+            res.append([1, i + a, 1])
+            a = i + a
+            i += 1
+
+    return res
+
+print(generate(4))

+ 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)
 

BIN
tree/__pycache__/tree.cpython-38.pyc


+ 20 - 0
tree/sortedArray_to_bst.py

@@ -0,0 +1,20 @@
+from typing import List, Optional
+from tree import TreeNode
+
+
+class Solution:
+    def sortedArrayToBST(self, nums: List[int]) -> Optional[TreeNode]:
+        if not nums:
+            return
+        mid = len(nums) // 2
+        root = TreeNode(nums[mid])
+
+        if mid == 0:
+            return root
+
+        root.left = self.sortedArrayToBST(nums[:mid])
+        root.right = self.sortedArrayToBST(nums[mid + 1:])
+
+        return root
+
+

+ 5 - 138
tree/tree.py

@@ -1,142 +1,9 @@
-#列表函数
-def BinaryTree(r):
-    return [r, [], []]
 
-#插入左子树
-def insertLeft(root, newBranch):
-    t = root.pop(1)
-    if len(t) > 1:
-        root.insert(1, [newBranch, t, []])
-    else:
-        root.insert(1, [newBranch, [], []])
-    return root
-
-#插入右子树
-def insertRight(root, newBranch):
-    t = root.pop(2)
-    if len(t) > 1:
-        root.insert(2, [newBranch, [], t])
-    else:
-        root.insert(2, [newBranch, [], []])
-    return root
-
-#树的访问函数
-def getRootVal(root):
-    return root[0]
-
-def setRootVal(root, newval):
-    root[0] = newval
-
-def getLeftChild(root):
-    return root[1]
-
-def getRightChild(root):
-    return root[2]
-
-
-# 二叉树的访问函数
-def getLeftChild(self):
-    return self.leftChild
-
-def getRightChild(self):
-    return self.rightChild
-
-def setRootValue(self, obj):
-    self.key = obj
-
-def getRootValue(self):
-    return self.key
-
-
-# 定义节点
-class BinaryTree:
-    def __init__(self, rootObj):
-        self.key = rootObj
-        self.leftChild = None    # 左子节点也是树
-        self.rightChild = None    # 右子节点也是树
-
-# 插入左子节点
-def insertLeft(self, newNode):
-    if self.leftChild == None:
-        self.leftChild = newNode
-    else:
-        t = BinaryTree(newNode)
-        t.leftChild = self.leftChild
-        self.leftChild = t
-
-# 插入右子节点
-def insertRight(self, newNode):
-    if self.rightChild == None:
-        self.rightChild = newNode
-    else:
-        t = BinaryTree(newNode)
-        t.rightChild = self.rightChild
-        self.rightChild = t
-
-# 树的遍历
-
-# 前序
-def preorder(tree):
-    if tree:
-        print(tree.getRootVal())
-        preorder(tree.getLeftChild())
-        preorder(tree.getRightChild())
-
-# 中序
-def inorder(tree):
-    if tree:
-        inorder(tree.getLeftChild())
-        print(tree.getRootVal())
-        inorder(tree.getRightChild())
-
-# 后序
-def postorder(tree):
-    if tree:
-        postorder(tree.getLeftChild())
-        postorder(tree.getRightChild())
-        print(tree.getRootVal())
-
-# 树
-# class Bnode(object):
-#     def __init__(self, data, left=None, right=None):
-#         self.data = data
-#         self.left = left
-#         self.right = right
-#
-# class Btree(object):
-#     def __init__(self, root=None):
-#         self.root = root
-#
-#     @classmethod
-#     def bulid(cls, list):
-#         node_dict = {}
-#         # 制作节点
-#         for item in list:
-#             data = item['data']
-#             node_dict[data] = Bnode(data)
-#         # 把节点组装成数
-#         for item in list:
-#             data = item['data']
-#             node = node_dict[data]
-#             if node['is_root']:
-#                 root = node
-#             node.left = node_dict.get(item['left'])
-#             node.right = node_dict.get(item['right'])
-#         return cls(root)
-#
-#     def preorder(self, subtree):
-#         # 先序
-#         if subtree is not None:
-#             print(subtree.data)
-#             self.preorder(subtree.left)
-#             self.preorder(subtree.right)
-#
-#     def reverse(self, subtree):
-#         # 后序
-#         if subtree is not None:
-#             subtree.left, subtree.right = subtree.right, subtree.left
-#             self.reverse(self, subtree.left)
-#             self.reverse(self, subtree.right)
+class TreeNode:
+    def __init__(self, val=0, left=None, right=None):
+        self.val = val
+        self.left = left
+        self.right = right
 
 # 回溯通用模板
 # res = []