摘要: 自己写的 class Solution: def constructRectangle(self, area: int) -> List[int]: # 计算给定面积的平方根 root = area ** 0.5 # 初始化结果列表,默认为 [1, area],即长为面积,宽为1的情况 temp = 阅读全文
posted @ 2024-05-14 11:06 Junior_bond 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 自己写的: from typing import List class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) -> int: # 初始化最大连续1的计数器和临时连续1的计数器 count = 0 temp = 0 # 阅读全文
posted @ 2024-05-13 10:45 Junior_bond 阅读(2) 评论(0) 推荐(0) 编辑
摘要: 自己写的: class Solution: def licenseKeyFormatting(self, s: str, k: int) -> str: # 将字符串转换为列表,方便操作 new_S = list() # 遍历输入字符串 for i in s: # 如果当前字符不是 '-',则添加到 阅读全文
posted @ 2024-05-12 16:34 Junior_bond 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 自己写的: class Solution: def findComplement(self, num: int) -> int: # 初始化一个空字符串,用于存储二进制表示 bin_str = '' # 将输入的数字转换为二进制表示,存储在 bin_str 中 while num > 0: bin_ 阅读全文
posted @ 2024-05-11 21:37 Junior_bond 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 自己写的: class Solution: def islandPerimeter(self, grid: List[List[int]]) -> int: # 初始化周长计数器 count = 0 # 遍历二维网格的行 for i in range(len(grid)): # 遍历二维网格的列 f 阅读全文
posted @ 2024-05-10 13:34 Junior_bond 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 自己写的,调用了bin()函数 class Solution: def hammingDistance(self, x: int, y: int) -> int: # 对 x 和 y 进行异或运算,并将结果转换为二进制字符串 bin_yihuo = bin(x ^ y)[2:] # 统计二进制字符串 阅读全文
posted @ 2024-05-09 15:23 Junior_bond 阅读(1) 评论(0) 推荐(0) 编辑
摘要: 自己写的: class Solution: def repeatedSubstringPattern(self, s: str) -> bool: # 获取字符串的长度 n = len(s) # 初始化指针指向字符串的最后一个字符 rptr = n - 1 # 在指针到达字符串的第一个字符之前循环 阅读全文
posted @ 2024-05-08 18:07 Junior_bond 阅读(3) 评论(0) 推荐(0) 编辑
摘要: 自己写的,没有使用排序 ,会超出时间限制: 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 阅读(3) 评论(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) 编辑