04 2024 档案
摘要:自己写的: import math class Solution: def arrangeCoins(self, n: int) -> int: # 计算判别式 discriminant = 1 + 8 * n # 计算根 root1 = (-1 + math.sqrt(discriminant))
阅读全文
摘要:class Solution: def countSegments(self, s: str) -> int: n = len(s) # 如果字符串长度为1且不是空格,则只有一个段 if n == 1 and s[0] != ' ': return 1 # 如果字符串长度为1且是空格,则没有段 if
阅读全文
摘要:自己写的: class Solution: def addStrings(self, num1: str, num2: str) -> str: # 将两个字符串转换为整数 num1_int = self.strToInt(num1) num2_int = self.strToInt(num2) #
阅读全文
摘要:自己写的,用时很长 from typing import List class Solution: def thirdMax(self, nums: List[int]) -> int: # 计算输入列表的长度 n = len(nums) # 对列表进行冒泡排序,将较大的元素排在前面 for i i
阅读全文
摘要:自己写的 from typing import List class Solution: def fizzBuzz(self, n: int) -> List[str]: # 初始化结果列表 res = [] # FizzBuzz 对应的字符串列表 myli = ["Fizz", "Buzz", "
阅读全文
摘要:自己写的: class Solution: def longestPalindrome(self, s: str) -> int: count = 0 # 用于计算最长回文串的长度 hash = {} # 用于统计每个字符出现的次数的字典 # 统计每个字符出现的次数 for i in s: if n
阅读全文
摘要:自己写的,先整数转二进制,再切片二进制转16进制 class Solution: def toHex(self, num: int) -> str: # 处理特殊情况:当 num 为 0 时,直接返回 '0' if num == 0: return '0' # 定义十六进制字母的映射关系 my_di
阅读全文
摘要:自己写的,使用了经典的广度优先搜素(BFS): class Solution: def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int: # 初始化队列,将根节点放入队列中 queue = [root] # 初始化结果变量 res = 0
阅读全文
摘要:自己写的,调用了combinations函数: from itertools import combinations from typing import List class Solution: def readBinaryWatch(self, turnedOn: int) -> List[st
阅读全文
摘要:自己写的,有点麻烦 class Solution: def isSubsequence(self, s: str, t: str) -> bool: # 第一步先验证s是t的无序子序列 # 使用字典记录t中每个字符的出现次数 mydict = dict() for i in t: if not my
阅读全文
摘要:自己写的,哈希表,easy class Solution: def findTheDifference(self, s: str, t: str) -> str: # 创建一个空字典,用于存储字符出现的次数 mydict = {} # 遍历字符串s,统计每个字符出现的次数 for i in s: i
阅读全文
摘要:自己写的,easy class Solution: def firstUniqChar(self, s: str) -> int: mydict = {} # 创建一个空字典来存储每个字符的出现次数 for i in s: # 遍历给定的字符串 s if not mydict.get(i): # 如
阅读全文
摘要:class Solution: def guessNumber(self, n: int) -> int: i = 1 # 初始猜测数为1 flag = True # 设置一个标志,用于控制循环 # 第一部分:使用倍增法寻找一个大于目标数字的边界值 while flag: # 使用 guess 函数
阅读全文
摘要:用二分法查找平方根: class Solution: def isPerfectSquare(self, num: int) -> bool: # 初始化左右边界 left = 1 right = num # 开始二分查找 while left <= right: # 计算中间值 mid = lef
阅读全文
摘要:自己写的: from typing import List class Solution: def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]: # 如果 nums1 或 nums2 为空列表,则返回空列表 if
阅读全文
摘要:自己写的: from typing import List # 导入List类型,用于函数参数和返回类型的注解 class Solution: def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]: # 初始化
阅读全文
摘要:自己写的,双指针,一次通过 class Solution: def reverseVowels(self, s: str) -> str: # 将输入的字符串转换为列表 s_list = list(s) # 定义元音字母列表 vowels = ['a', 'e', 'i', 'o', 'u', 'A
阅读全文
摘要:自己写的,这么简单? from typing import List class Solution: def reverseString(self, s: List[str]) -> None: n = len(s) # 获取字符串列表的长度 # 使用双指针法来反转字符串 # 初始化指针i指向字符串
阅读全文
摘要:自己写的,如果n对4可以整除。就让n除以4,循环往复 class Solution: def isPowerOfFour(self, n: int) -> bool: while n%4==0 and n>0: n//=4 return n==1
阅读全文
摘要:自己写的: from typing import List class Solution: def countBits(self, n: int) -> List[int]: # 创建一个空列表来存储结果 result = [] # 循环遍历从0到n的所有数字 for i in range(n +
阅读全文
摘要:自己写的,耗时很长 class Solution: def isPowerOfThree(self, n: int) -> bool: # 如果n为负数,则不是3的幂 if n < 0: return False # 如果n为1,则是3的幂 if n == 1: return True # 如果n为
阅读全文
摘要:自己写的,耗时很长 class NumArray: def __init__(self, nums: List[int]): # 初始化NumArray类,接收一个整数列表nums作为参数 self.nums = nums # 将传入的nums列表存储为对象的属性 def sumRange(self
阅读全文