上一页 1 2 3 4 5 6 7 8 9 ··· 14 下一页
摘要: 自己写的,耗时很长 class Solution: def isPowerOfThree(self, n: int) -> bool: # 如果n为负数,则不是3的幂 if n < 0: return False # 如果n为1,则是3的幂 if n == 1: return True # 如果n为 阅读全文
posted @ 2024-04-02 13:21 Junior_bond 阅读(9) 评论(0) 推荐(0)
摘要: 自己写的,耗时很长 class NumArray: def __init__(self, nums: List[int]): # 初始化NumArray类,接收一个整数列表nums作为参数 self.nums = nums # 将传入的nums列表存储为对象的属性 def sumRange(self 阅读全文
posted @ 2024-04-01 13:51 Junior_bond 阅读(16) 评论(0) 推荐(0)
摘要: 自己写的,好像有点复杂: class Solution: def wordPattern(self, pattern: str, s: str) -> bool: # 将字符串 s 按空格切分成单词列表 mystr = s.split() # 创建一个空字典,用于存储字符和其出现位置的映射 mydi 阅读全文
posted @ 2024-03-29 13:54 Junior_bond 阅读(22) 评论(0) 推荐(0)
摘要: 自己写的,冒泡排序,时间爆炸 class Solution: def moveZeroes(self, nums) -> None: """ Do not return anything, modify nums in-place instead. """ n=len(nums) for i in 阅读全文
posted @ 2024-03-28 12:52 Junior_bond 阅读(6) 评论(0) 推荐(0)
摘要: 自己写的二分法: class Solution: def firstBadVersion(self, n: int) -> int: # 初始化左右指针进行二分查找 left = 1 right = n # 检查第一个版本是否是坏版本 if isBadVersion(1): return 1 # 检 阅读全文
posted @ 2024-03-27 18:44 Junior_bond 阅读(11) 评论(0) 推荐(0)
摘要: 自己写的,调包排序: from typing import List class Solution: def missingNumber(self, nums: List[int]) -> int: # 将输入的列表排序 nums_new = sorted(nums) # 如果排序后的列表的第一个元 阅读全文
posted @ 2024-03-26 14:27 Junior_bond 阅读(13) 评论(0) 推荐(0)
摘要: 丑数 就是只包含质因数 2、3 和 5 的正整数。换句话说 丑数=2*……*2*3*……*3*5*……*5 class Solution: def isUgly(self, n: int) -> bool: # 如果 n 小于等于 0,则不是丑数,返回 False if n <= 0: return 阅读全文
posted @ 2024-03-25 14:17 Junior_bond 阅读(14) 评论(0) 推荐(0)
摘要: 三个简单函数: class Solution: def addDigits(self, num: int) -> int: # 如果数字已经是个位数,则直接返回 if num <= 9: return num # 持续执行直到数字变成个位数为止 while True: # 将数字转换为其各个数字构成 阅读全文
posted @ 2024-03-25 13:52 Junior_bond 阅读(17) 评论(0) 推荐(0)
摘要: 迭代法-深度优先搜索(栈) class Solution: def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]: if not root: return [] # 如果根节点为空,直接返回空列表 stack = [(roo 阅读全文
posted @ 2024-03-23 18:41 Junior_bond 阅读(12) 评论(0) 推荐(0)
摘要: 自己写的,有字典很简单: class Solution: def isAnagram(self, s: str, t: str) -> bool: # 创建两个空字典来存储字符计数 s_dic = dict() t_dic = dict() # 遍历字符串s,计算每个字符出现的次数并存储在s_dic 阅读全文
posted @ 2024-03-23 17:52 Junior_bond 阅读(8) 评论(0) 推荐(0)
上一页 1 2 3 4 5 6 7 8 9 ··· 14 下一页