提交 7a442c65 编写于 作者: 檀越@新空间's avatar 檀越@新空间 🐭

fix:二进制的题

上级 1a05b946
"""
回文链表
"""
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)
"""
链表的中间结点
"""
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
"""
二进制链表转整数
"""
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
"""
设计哈希集合
"""
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)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册