LeetCode 每日一题 2025/1/27-2025/2/2

news/2025/2/9 3:34:07 标签: leetcode, 算法, python

记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步


目录

      • 1/27 45. 跳跃游戏 II
      • 1/28 119. 杨辉三角 II
      • 1/29 219. 存在重复元素 II
      • 1/30 350. 两个数组的交集 II
      • 1/31 541. 反转字符串 II
      • 2/1 81. 搜索旋转排序数组 II
      • 2/2 598. 区间加法 II


1/27 45. 跳跃游戏 II

题目说明一定可以跳到队末 要次数最少
找到跳到队尾的最靠前的一个点 将队尾移到这个点 跳动次数+1 pos记录当前的地址
反复寻找
begin用来记录重新开始寻找的点
如果开头是1则直接进行跳跃 次数+1 begin+1 及略过开头若干个1

python">def jump(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    begin = 0
    pos = 0 
    num = 0
    l = len(nums)
    if l<2:
        return num
    
    while True:
        while nums[pos]==1:
            pos+=1
            if pos==l-1:
                break
            num+=1
            begin = pos
        while pos+nums[pos]<l-1:
            pos+=1
        num +=1
        if pos==begin or pos==l-1:
            break
        l = pos+1
        pos = begin
    return num



1/28 119. 杨辉三角 II

按规则生成

python">def getRow(rowIndex):
    """
    :type rowIndex: int
    :rtype: List[int]
    """
    if rowIndex==0:
        return [1]
    elif rowIndex==1:
        return [1,1]
    pre = [1,1]
    ind = 1
    while ind<rowIndex:
        ind+=1
        cur = [1]
        for i in range(len(pre)-1):
            cur.append(pre[i]+pre[i+1])
        cur.append(1)
        pre=cur[:]
    return cur



1/29 219. 存在重复元素 II

滑动窗口 保持k+1长度 记录长度内出现的数字个数
如果数字个数大于1 则成立

python">def containsNearbyDuplicate( nums, k):
    """
    :type nums: List[int]
    :type k: int
    :rtype: bool
    """
    from collections import defaultdict
    win = defaultdict(int)
    for i in range(min(len(nums),k+1)):
        win[nums[i]]+=1
        if win[nums[i]]>1:
            return True
    for i in range(k+1,len(nums)):
        win[nums[i-k-1]]-=1
        win[nums[i]]+=1
        if win[nums[i]]>1:
            return True
    return False



1/30 350. 两个数组的交集 II

ans1: 接349 获取不重复的交集l
遍历l中的元素
找到两个集合中该元素出现最少的次数 添加进答案
ans2: 将第一个集合编成 字符-次数 的字典 遍历第二个集合
如果某一个字符在字典中并且次数不为0 则加入 字典中的次数减一

python">from collections import defaultdict

def intersect(nums1, nums2):
    """
    :type nums1: List[int]
    :type nums2: List[int]
    :rtype: List[int]
    """
    n1=set(nums1)
    n2=set(nums2)
    l = list(n1&n2)
    res=[]
    for x in l:
        num = min(nums1.count(x),nums2.count(x))
        res.extend([x]*num)
    return res

def intersect2(nums1, nums2):
    """
    :type nums1: List[int]
    :type nums2: List[int]
    :rtype: List[int]
    """
    d = defaultdict(int)
    res=[]
    for i in nums1:
        d[i]+=1
    for i in nums2:
        if d[i]>0:
            res.append(i)
            d[i]-=1
    return res



1/31 541. 反转字符串 II

l,r代表需要反正的字符段左右
l需要小于s的长度 r为l+k-1和长度n-1之间的较小值
下一个l为之前r+k+1

python">def reverseStr(s, k):
    """
    :type s: str
    :type k: int
    :rtype: str
    """
    n = len(s)
    ls= list(s)
    l = 0
    while l<n:
        r = min(l+k-1,n-1)
        tmp = r
        while l<r:
            ls[l],ls[r]=ls[r],ls[l]
            l+=1
            r-=1
        l = tmp+k+1
    return ''.join(ls)



2/1 81. 搜索旋转排序数组 II

找到当前旋转的位置 nums[n-1]>=nums[0]
恢复原来顺序 再进行二分查找

python">def search(nums, target):
    """
    :type nums: List[int]
    :type target: int
    :rtype: bool
    """
    def find(nums):
        l,r = 0,len(nums)-1
        while l<=r:
            mid = l +(r-l)//2
            if nums[mid]==target:
                return True
            elif nums[mid]<target:
                l = mid+1
            else:
                r = mid-1
        return False
    if len(nums)==1:
        return nums[0]==target
    if len(nums)==2:
        return nums[0]==target or nums[1]==target
    
    if nums[0]<nums[-1]:
        return find(nums)
    
    for i in range(1,len(nums)):
        if nums[i]<nums[i-1]:
            return find(nums[i:]+nums[:i])
    return False



2/2 598. 区间加法 II

最大整数即为每次都加到的区间
遍历记录x,y的最小值

python">def maxCount(m, n, ops):
    """
    :type m: int
    :type n: int
    :type ops: List[List[int]]
    :rtype: int
    """
    curx,cury=m,n
    for x,y in ops:
        curx = min(curx,x)
        cury = min(cury,y)
    return curx*cury




http://www.niftyadmin.cn/n/5845492.html

相关文章

Kotlin协程详解——协程取消与超时

目录 一、协程取消 1.取消协程的执行 2.使计算代码可取消 3.在finally中释放资源 4.运行不能取消的代码块 二、协程超时 异步超时与资源管理 一、协程取消 1.取消协程的执行 在一个长时间运行的应用程序中&#xff0c;你也许需要对你的后台协程进行细粒度的控制。 比如…

LeetCode 72.编辑距离

要解决LeetCode 72题“编辑距离”问题&#xff0c;我们可以使用动态规划的方法。以下是详细的C解题思路和实现过程&#xff1a; 解题思路 问题分析 编辑距离问题要求将字符串 word1 转换为 word2 所需的最少操作次数&#xff0c;允许的操作包括插入、删除和替换字符。动态规划…

零基础学习书生.浦语大模型--基础岛

第二关:玩转书生[多模态对话]和[AI搜索]产品 任务一&#xff1a;使用MindSearch 任务二&#xff1a;尝试使用书生.浦语 尝试让其写一段Self-Attention网络模块代码 import torch import torch.nn as nn import torch.nn.functional as Fclass SelfAttention(nn.Module):def _…

Explain 是 SQL 查询优化中非常重要的工具,它用于分析 SQL 查询的执行计划

Explain 是 SQL 查询优化中非常重要的工具&#xff0c;它用于分析 SQL 查询的执行计划https://mp.weixin.qq.com/s/QKra-Sp5JoaEPSCqfffOtA

Spring Cloud 01 - 微服务概述

微服务概述 文章目录 微服务概述一&#xff1a;分布式和微服务1、什么是微服务&#xff1f;2&#xff1a;什么是分布式3、联系与区别是什么&#xff1f; 2&#xff1a;最终技术搭配方案 一&#xff1a;分布式和微服务 1、什么是微服务&#xff1f; 微服务架构是团队面对互联网…

centos虚拟机迁移没有ip的问题

故事背景&#xff0c;我们的centos虚拟机本来是好好的&#xff0c;但是拷贝到其他电脑上就不能分配ip&#xff0c;我个人觉得这个vmware他们软件应该搞定这个啊&#xff0c;因为这个问题是每次都会出现的。 网络选桥接 网络启动失败 service network restart Restarting netw…

数据库基础练习4(有关索引,视图完整解答)

建立需要的表 学生表 mysql> create table studnet(sno int primary key auto_increment,sname varchar(30) not null unique,ssex varchar(2) check (ssex男 or ssex女) not null ,sage int not null,sdept varchar(10) default 计算机 not null); Query OK, 0 rows affe…

3. 【.NET Aspire 从入门到实战】--理论入门与环境搭建--环境搭建

构建现代云原生应用程序时&#xff0c;开发环境的搭建至关重要。NET Aspire 作为一款专为云原生应用设计的开发框架&#xff0c;提供了一整套工具、模板和集成包&#xff0c;旨在简化分布式系统的构建和管理。开始项目初始化之前&#xff0c;确保开发环境的正确配置是成功的第一…