摘要: 自己写的,没有使用排序 ,会超出时间限制: class Solution: def findContentChildren(self, g: List[int], s: List[int]) -> int: if len(s)==0: return 0 count=0 for i in range( 阅读全文
posted @ 2024-05-07 14:42 Junior_bond 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 自己写的,利用集合的差 class Solution: def findDisappearedNumbers(self, nums: List[int]) -> List[int]: # 计算 nums 列表的长度 n = len(nums) # 创建包含 1 到 n 的列表 mylist = [i 阅读全文
posted @ 2024-05-06 12:34 Junior_bond 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 自己写的: import math class Solution: def arrangeCoins(self, n: int) -> int: # 计算判别式 discriminant = 1 + 8 * n # 计算根 root1 = (-1 + math.sqrt(discriminant)) 阅读全文
posted @ 2024-04-30 14:56 Junior_bond 阅读(1) 评论(0) 推荐(0) 编辑
摘要: class Solution: def countSegments(self, s: str) -> int: n = len(s) # 如果字符串长度为1且不是空格,则只有一个段 if n == 1 and s[0] != ' ': return 1 # 如果字符串长度为1且是空格,则没有段 if 阅读全文
posted @ 2024-04-29 14:01 Junior_bond 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 自己写的: class Solution: def addStrings(self, num1: str, num2: str) -> str: # 将两个字符串转换为整数 num1_int = self.strToInt(num1) num2_int = self.strToInt(num2) # 阅读全文
posted @ 2024-04-28 13:19 Junior_bond 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 自己写的,用时很长 from typing import List class Solution: def thirdMax(self, nums: List[int]) -> int: # 计算输入列表的长度 n = len(nums) # 对列表进行冒泡排序,将较大的元素排在前面 for i i 阅读全文
posted @ 2024-04-26 14:06 Junior_bond 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 自己写的 from typing import List class Solution: def fizzBuzz(self, n: int) -> List[str]: # 初始化结果列表 res = [] # FizzBuzz 对应的字符串列表 myli = ["Fizz", "Buzz", " 阅读全文
posted @ 2024-04-25 19:12 Junior_bond 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 自己写的: class Solution: def longestPalindrome(self, s: str) -> int: count = 0 # 用于计算最长回文串的长度 hash = {} # 用于统计每个字符出现的次数的字典 # 统计每个字符出现的次数 for i in s: if n 阅读全文
posted @ 2024-04-24 22:07 Junior_bond 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 自己写的,先整数转二进制,再切片二进制转16进制 class Solution: def toHex(self, num: int) -> str: # 处理特殊情况:当 num 为 0 时,直接返回 '0' if num == 0: return '0' # 定义十六进制字母的映射关系 my_di 阅读全文
posted @ 2024-04-23 16:40 Junior_bond 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 自己写的,使用了经典的广度优先搜素(BFS): class Solution: def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int: # 初始化队列,将根节点放入队列中 queue = [root] # 初始化结果变量 res = 0 阅读全文
posted @ 2024-04-23 13:59 Junior_bond 阅读(1) 评论(0) 推荐(0) 编辑