上一页 1 2 3 4 5 6 7 8 9 10 ··· 14 下一页
摘要: 毫无创意的一版: # 定义一个类 Solution class Solution: # 定义一个方法 isPalindrome,用于检查链表是否为回文 def isPalindrome(self, head: Optional[ListNode]) -> bool: # 如果链表为空,则它是一个回文 阅读全文
posted @ 2024-03-21 13:52 Junior_bond 阅读(16) 评论(0) 推荐(0)
摘要: 自己写的: class MyQueue: def __init__(self): self.li=list() def push(self, x: int) -> None: self.li.append(x) def pop(self) -> int: return self.li.pop(0) 阅读全文
posted @ 2024-03-20 09:33 Junior_bond 阅读(7) 评论(0) 推荐(0)
摘要: 递归写法: class Solution: def isPowerOfTwo(self, n: int) -> bool: # 如果 n 等于 1,则直接返回 True if n == 1: return True # 如果 n 等于 0,则直接返回 False if n == 0: return 阅读全文
posted @ 2024-03-19 10:22 Junior_bond 阅读(11) 评论(0) 推荐(0)
摘要: 自己写的: class Solution: def summaryRanges(self, nums): my_li = [] # 创建一个空列表用于存储结果 if not nums: # 如果输入列表为空 return my_li # 返回空列表 if len(nums) == 1: # 如果输入 阅读全文
posted @ 2024-03-18 10:10 Junior_bond 阅读(9) 评论(0) 推荐(0)
摘要: 自己写的: class Solution: def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]: # 创建一个新的 TreeNode 以存储反转后的树 newroot = TreeNode() # 如果输入的根节点 阅读全文
posted @ 2024-03-16 16:56 Junior_bond 阅读(7) 评论(0) 推荐(0)
摘要: 利用内置的list class MyStack: def __init__(self): # 初始化一个空列表用于存储栈的元素 self.li = list() def push(self, x: int) -> None: # 向栈中压入元素 x self.li.append(x) def pop 阅读全文
posted @ 2024-03-15 13:29 Junior_bond 阅读(9) 评论(0) 推荐(0)
摘要: 自己写的,使用广度优先BFS,迭代: class Solution: def countNodes(self, root: Optional[TreeNode]) -> int: # 如果根节点为空,则树中节点数为 0 if not root: return 0 # 初始化队列,并将根节点放入队列中 阅读全文
posted @ 2024-03-14 18:16 Junior_bond 阅读(12) 评论(0) 推荐(0)
摘要: 自己写的: from typing import List class Solution: def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool: # 创建一个空字典,用于存储数字及其最近出现的索引 mydict = { 阅读全文
posted @ 2024-03-13 10:07 Junior_bond 阅读(4) 评论(0) 推荐(0)
摘要: 暴力解法 秒杀 class Solution: def containsDuplicate(self, nums: List[int]) -> bool: # 使用 set 将列表转换为集合,去除重复元素 unique_set = set(nums) # 如果集合的长度和原始列表的长度相等,说明没有 阅读全文
posted @ 2024-03-12 21:38 Junior_bond 阅读(5) 评论(0) 推荐(0)
摘要: 自己写的,遍历一遍链表,再反向生成一个新链表 class Solution: def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]: # 检查链表是否为空 if not head: return None # 初始 阅读全文
posted @ 2024-03-11 10:16 Junior_bond 阅读(5) 评论(0) 推荐(0)
上一页 1 2 3 4 5 6 7 8 9 10 ··· 14 下一页