From 7a442c6561debd62f5aecb76bbfd5b3eab56d0c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Fri, 21 Jul 2023 18:22:02 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BA=8C=E8=BF=9B=E5=88=B6=E7=9A=84?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../problem_solving_09.py" | 49 +++++++++++++++++++ .../problem_solving_10.py" | 45 +++++++++++++++++ .../problem_solving_01.py" | 48 ++++++++++++++++++ .../problem_solving_10.py" | 43 ++++++++++++++++ 4 files changed, 185 insertions(+) create mode 100644 "02-\351\223\276\350\241\250/problem_solving_09.py" create mode 100644 "02-\351\223\276\350\241\250/problem_solving_10.py" create mode 100644 "06-\344\272\214\350\277\233\345\210\266/problem_solving_01.py" create mode 100644 "888-\350\256\276\350\256\241\351\242\230/problem_solving_10.py" diff --git "a/02-\351\223\276\350\241\250/problem_solving_09.py" "b/02-\351\223\276\350\241\250/problem_solving_09.py" new file mode 100644 index 0000000..562af38 --- /dev/null +++ "b/02-\351\223\276\350\241\250/problem_solving_09.py" @@ -0,0 +1,49 @@ +""" +回文链表 +""" +from typing import Optional + + +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + +class Solution: + def isPalindrome(self, head: Optional[ListNode]) -> bool: + """ + 回文链表 + :param head: + :return: + """ + contain = [] + curr = head + while curr is not None: + contain.append(curr.val) + curr = curr.next + curr2 = head + while curr2 is not None: + if contain.pop() != curr2.val: + return False + curr2 = curr2.next + return True + + +if __name__ == '__main__': + # 1, 2, 6, 3, 4, 5, 6 + l1 = ListNode(1) + l1.next = ListNode(2) + l1.next.next = ListNode(3) + l1.next.next.next = ListNode(3) + l1.next.next.next.next = ListNode(2) + l1.next.next.next.next.next = ListNode(1) + # l1.next.next.next.next.next.next = ListNode(7) + # l2 = ListNode(5) + # l2.next = ListNode(6) + # l2.next.next = ListNode(1) + # l2.next.next.next = ListNode(8) + # l2.next.next.next.next = ListNode(4) + # l2.next.next.next.next.next = ListNode(5) + result = Solution().isPalindrome(l1) + print(result) diff --git "a/02-\351\223\276\350\241\250/problem_solving_10.py" "b/02-\351\223\276\350\241\250/problem_solving_10.py" new file mode 100644 index 0000000..46d0c91 --- /dev/null +++ "b/02-\351\223\276\350\241\250/problem_solving_10.py" @@ -0,0 +1,45 @@ +""" +链表的中间结点 +""" +from typing import Optional + + +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + +class Solution: + def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]: + """ + 链表的中间结点,如果有2个 返回第二个 快慢指针 + :param head: + :return: + """ + fast, slow = head, head + while fast and fast.next: + slow = slow.next + fast = fast.next.next + return slow + + +if __name__ == '__main__': + # 1, 2, 6, 3, 4, 5, 6 + l1 = ListNode(1) + l1.next = ListNode(2) + l1.next.next = ListNode(3) + l1.next.next.next = ListNode(3) + l1.next.next.next.next = ListNode(2) + l1.next.next.next.next.next = ListNode(1) + # l1.next.next.next.next.next.next = ListNode(7) + # l2 = ListNode(5) + # l2.next = ListNode(6) + # l2.next.next = ListNode(1) + # l2.next.next.next = ListNode(8) + # l2.next.next.next.next = ListNode(4) + # l2.next.next.next.next.next = ListNode(5) + result = Solution().middleNode(l1) + while result: + print(result.val, end=' -> ') + result = result.next diff --git "a/06-\344\272\214\350\277\233\345\210\266/problem_solving_01.py" "b/06-\344\272\214\350\277\233\345\210\266/problem_solving_01.py" new file mode 100644 index 0000000..cf73b17 --- /dev/null +++ "b/06-\344\272\214\350\277\233\345\210\266/problem_solving_01.py" @@ -0,0 +1,48 @@ +""" +二进制链表转整数 +""" +from typing import Optional + + +class ListNode: + def __init__(self, val=0, next=None): + self.val = val + self.next = next + + +class Solution: + def getDecimalValue(self, head: ListNode) -> int: + """ + 二进制链表转整数:头节点是高位 + :param head: + :return: + """ + res = 0 + while head: + # *2相当于左移一位 + res = res * 2 + head.val + head = head.next + return res + + +if __name__ == '__main__': + # 头节点是高位 + # 1, 2, 6, 3, 4, 5, 6 + l1 = ListNode(1) + l1.next = ListNode(0) + l1.next.next = ListNode(1) + # l1.next.next.next = ListNode(3) + # l1.next.next.next.next = ListNode(2) + # l1.next.next.next.next.next = ListNode(1) + # l1.next.next.next.next.next.next = ListNode(7) + # l2 = ListNode(5) + # l2.next = ListNode(6) + # l2.next.next = ListNode(1) + # l2.next.next.next = ListNode(8) + # l2.next.next.next.next = ListNode(4) + # l2.next.next.next.next.next = ListNode(5) + result = Solution().getDecimalValue(l1) + print(result) + # while result: + # print(result.val, end=' -> ') + # result = result.next diff --git "a/888-\350\256\276\350\256\241\351\242\230/problem_solving_10.py" "b/888-\350\256\276\350\256\241\351\242\230/problem_solving_10.py" new file mode 100644 index 0000000..83acc96 --- /dev/null +++ "b/888-\350\256\276\350\256\241\351\242\230/problem_solving_10.py" @@ -0,0 +1,43 @@ +""" +设计哈希集合 +""" + + +class MyHashSet: + + def __init__(self): + self.buckets = 1000 + self.itemsPerBucket = 1001 + self.table = [[] for _ in range(self.buckets)] + + def hash(self, key): + return key % self.buckets + + def pos(self, key): + return key // self.buckets + + def add(self, key): + hashkey = self.hash(key) + if not self.table[hashkey]: + self.table[hashkey] = [0] * self.itemsPerBucket + self.table[hashkey][self.pos(key)] = 1 + + def remove(self, key): + hashkey = self.hash(key) + if self.table[hashkey]: + self.table[hashkey][self.pos(key)] = 0 + + def contains(self, key): + hashkey = self.hash(key) + return (self.table[hashkey] != []) and (self.table[hashkey][self.pos(key)] == 1) + + +if __name__ == '__main__': + # 1, 2, 6, 3, 4, 5, 6 + hash_set = MyHashSet() + result1 = hash_set.add(1) + result2 = hash_set.add(2) + result3 = hash_set.contains(1) + print(result1) + print(result2) + print(result3) -- GitLab