2024.3.(9,10号)力扣(<=1200)刷题记录(未完)

本文详细探讨了数组串联、三角形类型判断、数字位数统计、分类求和、员工目标工作时长问题以及泰波那契数的几种解决方法,涉及遍历、插入数组、数学公式、动态规划和打表技巧。
摘要由CSDN通过智能技术生成

一、1929. 数组串联

1.遍历

class Solution:
    def getConcatenation(self, nums: List[int]) -> List[int]:
        # 遍历
        n = len(nums)
        ans = [0] * (2*n)
        for i in range(n):
            ans[i] = nums[i]
            ans[i+n] = nums[i]
        return ans

2.插入数组

class Solution:
    def getConcatenation(self, nums: List[int]) -> List[int]:
        # 插入数组
        ans = nums
        ans.extend(nums)    #extend()时间复杂度O(n)
        return ans

两种方式时间复杂度相同。

3.数组拼接

另外还看到别人的另一种解法,时间复杂度也为O(n),如下:

class Solution:
    def getConcatenation(self, nums: List[int]) -> List[int]:
        return nums + nums
        # return nums * 2
        # 返回新列表,原列表不变

二、3024. 三角形类型

排序

class Solution:
    def triangleType(self, nums: List[int]) -> str:
        if nums[0] == nums[1] == nums[2]:
            return "equilateral"
        nums.sort()
        if nums[0] + nums[1] > nums[2]:
            # 排序后只需比较两小边的和大不大于第三边
            if nums[0] == nums[1] or nums[1] == nums[2]:
                return "isosceles"
            return "scalene"
        return "none"

三、1295. 统计位数为偶数的数字 

1.转化为字符串,长度即为位数

class Solution:
    def findNumbers(self, nums: List[int]) -> int:
        # 转化为字符串
        chars = list(map(str,nums))
        ans = 0
        for c in chars:
            if len(c) % 2 == 0:
                ans += 1
        return ans

官方题解(. - 力扣(LeetCode))中同一方法的另一种写法:

class Solution:
    def findNumbers(self, nums: List[int]) -> int:
        # 转化为字符串 生成器
        return sum(1 for num in nums if len(str(num)) % 2 == 0)
        # return sum(len(str(num)) % 2 == 0 for num in nums)

2.运用数学知识取对数

这是在官方题解中看到的解法,学习一下:

class Solution:
    def findNumbers(self, nums: List[int]) -> int:
        # 运用数学知识,取以10为底的对数
        return sum(int(math.log10(num) + 1) % 2 == 0 for num in nums)   #int()向下取整,所以+1

3.特殊解法(仅针对该题)

看到官方题解下“sin猪”用户的评论(. - 力扣(LeetCode)),简直太妙了哈哈哈,记录一下。因为题目给出了范围,可以直接将情况列出来。代码如下:

class Solution:
    def findNumbers(self, nums: List[int]) -> int:
        # 特殊解法
        return sum(10 <= num <= 99 or 1000 <= num <= 9999 or num == 100000 for num in nums)

四、2894. 分类求和并作差

1.遍历法

先假设所有的数均为不能被m整除的数,那么num1-num2就等于前n项等差数列求和。然后再遍历每一个数,一旦有能被m整除的数,ans就减去2倍i(因为num1-i,num2+i,结果就-2i)。代码如下:

class Solution:
    def differenceOfSums(self, n: int, m: int) -> int:
        # 遍历
        ans = (1+n)*n//2
        for i in range(1,n+1):
            if i % m == 0:
                ans -= 2*i
        return ans

2.数学公式

这是灵神的方法(. - 力扣(LeetCode)),时复O(1)。来学习一下:

class Solution:
    def differenceOfSums(self, n: int, m: int) -> int:
        # 灵神代码 数学公式
        return n*(n+1)//2 - (n//m)*(n//m+1)*m

五、2798. 满足目标工作时长的员工数目 

1.遍历

class Solution:
    def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
        # 遍历
        ans = 0
        for i in range(len(hours)):
            if hours[i] >= target:
                ans += 1
        return ans

2.排序+遍历

虽然我这样用排序好像用处不太大,但是想到了就写了(不过运行速度要比直接遍历快)。

class Solution:
    def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
        # 排序,遍历
        hours.sort()
        for i in range(len(hours)):
            if hours[i] >= target:
                return len(hours) - i
        return 0

3.排序+查找函数

受下面这段代码的启发(未知用户):

class Solution:
    def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
        hours.append(target)
        hours.sort()
        return len(hours)-hours.index(target)-1

更改代码如下:

class Solution:
    def numberOfEmployeesWhoMetTarget(self, hours: List[int], target: int) -> int:
        # 排序,函数查找
        hours.sort()
        return len(hours) - bisect_left(hours,target)

bisect_left()函数时复为O(logn)。

六、1137. 第 N 个泰波那契数

1.遍历+移动窗口(动态规划)

class Solution:
    def tribonacci(self, n: int) -> int:
        # 遍历+移动窗口
        if n == 0:
            return 0
        if n == 1 or n == 2:
            return 1
        ans = [0] * 3
        ans[0],ans[1],ans[2] = 0,1,1
        for i in range(n-2):
            ans[0],ans[1],ans[2] = ans[1],ans[2],sum(ans)
        return ans[2]

2024.3.10续:

2.打表

学习参考宫水三叶题解(. - 力扣(LeetCode)),代码如下:

class Solution:
    # 打表
    # 在这里写为静态变量,避免多次打表
    # 通过写为类变量来模拟静态变量
    cache = [0] * 38    # n<= 37
    cache[0] = 0
    cache[1] = 1
    cache[2] = 1
    for i in range(3,38):
        cache[i] = cache[i-1] + cache[i-2] + cache[i-3]
    def tribonacci(self, n: int) -> int:
        return Solution.cache[n]

cache 是类变量,它在整个类的生命周期内只会被初始化一次。每个实例对象共享同一个 cache 数组,所以无论创建多少个实例对象,它们都会共享相同的 cache 数组。每个实例对象调用 tribonacci 函数时,都可以直接访问该共享的 cache 数组,而不会重新计算。这样可以节省内存,并且避免重复计算。————来自chatgpt

在python中模拟静态变量,学习记录:http://t.csdnimg.cn/SwwSl

3.矩阵快速幂

(仍在学习中)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值