02 2024 档案

摘要:递归法: class Solution: def __init__(self): # 初始化一个实例变量 res 用于存储前序遍历结果 self.res = [] def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]: 阅读全文
posted @ 2024-02-29 18:40 Junior_bond 阅读(5) 评论(0) 推荐(0) 编辑
摘要:快慢指针: class Solution: def hasCycle(self, head: Optional[ListNode]) -> bool: # 如果链表为空或者只有一个节点,肯定不存在环 if not head or not head.next: return False # 初始化慢指 阅读全文
posted @ 2024-02-28 10:47 Junior_bond 阅读(1) 评论(0) 推荐(0) 编辑
摘要:自己写的: from typing import List class Solution: def singleNumber(self, nums: List[int]) -> int: # 创建一个字典用于存储数字出现的次数 save_dict = dict() # 遍历输入列表 for i in 阅读全文
posted @ 2024-02-27 10:20 Junior_bond 阅读(3) 评论(0) 推荐(0) 编辑
摘要:自己写的: class Solution: def isPalindrome(self, s: str): # 将字符串转换为小写,以便进行大小写不敏感的比较 s_lower = s.lower() # 获取字符串的长度 n = len(s_lower) # 创建一个空列表,用于存储字母和数字 s_ 阅读全文
posted @ 2024-02-26 10:31 Junior_bond 阅读(8) 评论(0) 推荐(0) 编辑
摘要:二分查找——递归版: def binarySearch(aimlist, item): # 获取列表的长度 n = len(aimlist) # 如果列表非空 if n > 0: # 计算中间索引 mid = n // 2 # 如果中间元素是目标元素,则找到了 if aimlist[mid] == 阅读全文
posted @ 2024-02-25 21:17 Junior_bond 阅读(5) 评论(0) 推荐(0) 编辑
摘要:暴力解法,最后内存爆了 class Solution: def maxProfit(self, prices): n=len(prices) if n==1: return 0 if n>1000: return 3 profit = [] for i in range(n): cur=i+1 wh 阅读全文
posted @ 2024-02-25 19:18 Junior_bond 阅读(2) 评论(0) 推荐(0) 编辑
摘要:归并排序: def merge_sort(aimlist): #归并排序 拆分-排序-合并 也就是merge_返回的是是一个已经排好序的列表 n=len(aimlist) if n<=1: return aimlist mid=n//2 aimlist_left=merge_sort(aimlist 阅读全文
posted @ 2024-02-24 21:08 Junior_bond 阅读(5) 评论(0) 推荐(0) 编辑
摘要:自己写的: class Solution: def getRow(self, rowIndex: int): # 初始化一个列表以存储帕斯卡三角形的行 li = [] # 前两行是预定义的 li.append([1]) li.append([1, 1]) # rowIndex为0和1的基本情况 if 阅读全文
posted @ 2024-02-24 19:03 Junior_bond 阅读(4) 评论(0) 推荐(0) 编辑
摘要:自己写的: class Solution: def generate(self, numRows: int): # 初始化结果列表,包含帕斯卡三角形的前两行 li = [[1], [1, 1]] # 检查如果numRows为1或2,直接返回对应结果 if numRows == 1: return [ 阅读全文
posted @ 2024-02-23 16:22 Junior_bond 阅读(3) 评论(0) 推荐(0) 编辑
摘要:快速排序:递归 def quick_sort(aimlist, first, last): # 打印当前排序状态 print(aimlist) # 如果子列表只有一个元素或没有元素,直接返回 if first >= last: return # 初始化低位、高位和中间值 low = first he 阅读全文
posted @ 2024-02-22 21:49 Junior_bond 阅读(3) 评论(0) 推荐(0) 编辑
摘要:使用迭代: class Solution: def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool: # 如果根节点为空,直接返回False if not root: return False # 使用栈来进行迭代 阅读全文
posted @ 2024-02-22 15:00 Junior_bond 阅读(5) 评论(0) 推荐(0) 编辑
摘要:插入排序 def insrt_sort(aimlist): n=len(aimlist) for cur in range(1,n): i=cur while i>0: if aimlist[i]<aimlist[i-1]: aimlist[i],aimlist[i-1]=aimlist[i-1], 阅读全文
posted @ 2024-02-21 11:54 Junior_bond 阅读(15) 评论(0) 推荐(0) 编辑
摘要:冒泡排序: def bible_sort(aimlist): n=len(aimlist) j=len(aimlist) while j>0: for i in range(n-1): if aimlist[i]>aimlist[i+1]: aimlist[i],aimlist[i+1]=aimli 阅读全文
posted @ 2024-02-21 10:17 Junior_bond 阅读(2) 评论(0) 推荐(0) 编辑
摘要:栈的实现: class Stack(object): def __init__(self): self.__list=[] def push(self,item): self.__list.append(item) def pop(self): return self.__list.pop() de 阅读全文
posted @ 2024-02-21 10:14 Junior_bond 阅读(2) 评论(0) 推荐(0) 编辑
摘要:自己写的: # 二叉树节点的定义 class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right class Solution: 阅读全文
posted @ 2024-02-21 10:11 Junior_bond 阅读(2) 评论(0) 推荐(0) 编辑
摘要:对称二叉树走不通 201 / 228 个通过的测试用例 class Solution: def isBalanced(self, root: Optional[TreeNode]) -> bool: queue=[root] if not root: return True if not root. 阅读全文
posted @ 2024-02-20 11:49 Junior_bond 阅读(1) 评论(0) 推荐(0) 编辑
摘要:递归: class Solution: def process(self,left,right,nums): if left>right: return None mid=left+(right-left)//2 midnode=TreeNode(nums[mid]) midnode.left=se 阅读全文
posted @ 2024-02-19 13:00 Junior_bond 阅读(2) 评论(0) 推荐(0) 编辑
摘要:迭代法: class Solution: def maxDepth(self, root: Optional[TreeNode]) -> int: queue=[root] if not root: return 0 depth=0 while queue: level_len = len(queu 阅读全文
posted @ 2024-02-18 16:31 Junior_bond 阅读(5) 评论(0) 推荐(0) 编辑
摘要:迭代法: # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # 阅读全文
posted @ 2024-02-05 18:24 Junior_bond 阅读(3) 评论(0) 推荐(0) 编辑
摘要:自己写的有问题 即使先序+中序的结果一样 也不能确定同一棵树 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = 阅读全文
posted @ 2024-02-04 15:00 Junior_bond 阅读(2) 评论(0) 推荐(0) 编辑
摘要:自己写的递归: class Solution: def __init__(self): self.res_list=list() def inorderTraversal(self, root) : if root: if root==None: return else: self.inorderT 阅读全文
posted @ 2024-02-03 17:31 Junior_bond 阅读(2) 评论(0) 推荐(0) 编辑
摘要:二叉树 class Node(object): def __init__(self,val,lchild=None,rchild=None): self.val=val self.lchild=lchild self.rchild=rchild class Tree(object): def __i 阅读全文
posted @ 2024-02-03 16:57 Junior_bond 阅读(9) 评论(0) 推荐(0) 编辑
摘要:单循环链表 class ListNode: def __init__(self,val,next=None): self.val=val self.next=next class SingleLoopLinkList: def __init__(self,node=None): self.__hea 阅读全文
posted @ 2024-02-02 22:17 Junior_bond 阅读(5) 评论(0) 推荐(0) 编辑
摘要:双链表 class ListNode: def __init__(self,val,prev=None,next=None): self.val=val self.prev=prev self.next=next class DoubleLinkList: def __init__(self,nod 阅读全文
posted @ 2024-02-02 14:45 Junior_bond 阅读(3) 评论(0) 推荐(0) 编辑
摘要:使用 nums1[:m + n] = nums1_new 时,这是在原地修改 nums1 列表。具体来说,这个语句使用切片将 nums1 中前 m + n 个元素替换为 nums1_new 中的元素。这样做的结果是,nums1 的原始内存空间被修改,而不是创建一个新的列表对象。 使用 nums1 = 阅读全文
posted @ 2024-02-01 16:51 Junior_bond 阅读(6) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示