当前位置: 首页 > article >正文

LeetCode 每日一题 2025/2/3-2025/2/9

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


目录

      • 2/3 680. 验证回文串 II
      • 2/4 922. 按奇偶排序数组 II
      • 2/5 90. 子集 II
      • 2/6 47. 全排列 II
      • 2/7 59. 螺旋矩阵 II
      • 2/8 63. 不同路径 II
      • 2/9 80. 删除有序数组中的重复项 II


2/3 680. 验证回文串 II

头尾比较 可以有一次误差 遇到不一致时
分两种情况 去掉left或者去掉right 其中一种满足就可

def validPalindrome(s):
    """
    :type s: str
    :rtype: bool
    """
    def sub(s):
        if len(s)==0 or len(s)==1:
            return True
        i,j=0,len(s)-1
        while i<j:
            if s[i]!=s[j]:
                return False
            else:
                i+=1
                j-=1
        return True
    i,j=0,len(s)-1
    while i<j:
        if s[i]!=s[j]:
            return sub(s[i+1:j+1]) or sub(s[i:j])           
        else:
            i+=1
            j-=1
    return True

def validPalindrome2(s):
    """
    :type s: str
    :rtype: bool
    """
    if s==s[::-1]:
        return True
    i,j=0,len(s)-1
    while i<j:
        if s[i]==s[j]:
            i+=1
            j-=1
        else:
            tmp = s[:i]+s[i+1:]
            if tmp==tmp[::-1]:
                return True
            tmp = s[:j]+s[j+1:]
            if tmp==tmp[::-1]:
                return True
            return False




2/4 922. 按奇偶排序数组 II

  1. 分奇数偶数两个list 在偶数list中的奇数位 插入奇数
  2. 用ij记录偶数奇数位置 遍历list 如果是偶数放入i位置 奇数放入j位置
  3. 遍历一次数组
    使用ji,ou两个数组记录不符合未知的数
    遇到不符合位置的数时 从ji,ou数组中找到时候存在可交换的位置 有则交换 无则将该位置放入数组
def sortArrayByParityII(nums):
    """
    :type nums: List[int]
    :rtype: List[int]
    """
    odd = [x for x in nums if x%2==1]
    ans = [x for x in nums if x%2==0]
    
    p = 1
    for i in odd:
        ans.insert(p,i)
        p+=2
    return ans

def sortArrayByParityII2(nums):
    """
    :type nums: List[int]
    :rtype: List[int]
    """
    i,j=0,1
    ans = [0]*len(nums)
    for x in nums:
        if x%2==0:
            ans[i]=x
            i+=2
        else:
            ans[j]=x
            j+=2

def sortArrayByParityII3( nums):
    """
    :type nums: List[int]
    :rtype: List[int]
    """
    ji=[]
    ou=[]
    for i in range(len(nums)):
        if i%2!=nums[i]%2:
            if i%2==0:
                if len(ji)>0:
                    loc = ji.pop()
                    nums[loc],nums[i]=nums[i],nums[loc]
                else:
                    ou.append(i)
            else:
                if len(ou)>0:
                    loc = ou.pop()
                    nums[loc],nums[i]=nums[i],nums[loc]
                else:
                    ji.append(i)
    return nums



2/5 90. 子集 II

set 记录重复

def subsetsWithDup(nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    ret = []
    ret.append([])
    ck = set()
    for num in nums:
        l = []
        for tmp in ret:
            x = tmp[:]
            x.append(num)
            t = tuple(sorted(x))
            if t not in ck:
                l.append(x)
                ck.add(t)     
        ret.extend(l)
    return ret




2/6 47. 全排列 II

统计每个数字出现次数 回溯

def permuteUnique(nums):
    """
    :type nums: List[int]
    :rtype: List[List[int]]
    """
    m={}
    for i in nums:
        m[i] = m.get(i,0)+1
    n = len(nums)
    ret = []
    def back(m,l):
        if len(l)==n:
            ret.append(l)
            return
        for c in m:
            if m[c]>0:
                m[c]-=1
                l.append(c)
                back(m,l[:])
                l.pop()
                m[c]+=1
        
    back(m,[])
    return ret



2/7 59. 螺旋矩阵 II

顺指针可以 → ↓ ← ↑ 分别对应四种坐标变化(0,1),(1,0),(0,-1),(-1,0)
从→开始 如果遇到边界或者重复则改为下一种走法

def generateMatrix(n):
    """
    :type n: int
    :rtype: List[List[int]]
    """
    matrix = [[0]*n for _ in range(n)]
    steplist =[(0,1),(1,0),(0,-1),(-1,0)]
    step = 0
    
    num=2
    i,j=0,0
    matrix[0][0]=1
    while num<=n*n:
        x,y = steplist[step]
        
        ti,tj = i+x,j+y
        if ti<0 or tj<0 or ti>=n or tj>=n or matrix[ti][tj]>0:
            step = (step+1)%4
            continue
        i,j = ti,tj
        matrix[i][j] = num
        num+=1
    return matrix



2/8 63. 不同路径 II

抵达当前位置的路径数量为 左侧位置和上方位置数量之和

def uniquePathsWithObstacles(obstacleGrid):
    """
    :type obstacleGrid: List[List[int]]
    :rtype: int
    """
    m,n=len(obstacleGrid),len(obstacleGrid[0])
    ans = [[0]*n for _ in range(m)]
    for i in range(m):
        if obstacleGrid[i][0]==0:
            ans[i][0]=1
        else:
            break
    for j in range(n):
        if obstacleGrid[0][j]==0:
            ans[0][j]=1
        else:
            break
    for i in range(1,m):
        for j in range(1,n):
            if obstacleGrid[i][j]==0:
                ans[i][j]=ans[i-1][j]+ans[i][j-1]
    return ans[-1][-1]



2/9 80. 删除有序数组中的重复项 II

1.使用map记录重复
2.查看与前两位的比较 是否重复两次以上

def removeDuplicates(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    m={}
    loc =0
    for i in range(len(nums)):
        if m.get(nums[i],0)>=2:
            continue
        m[nums[i]] = m.get(nums[i],0)+1
        nums[loc] = nums[i]
        loc+=1
    return loc


def removeDuplicates2(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    if len(nums)<3:
        return len(nums)
    loc =2
    for i in range(2,len(nums)):
        if nums[loc-2]==nums[i]:
            continue
        nums[loc] = nums[i]
        loc+=1
    return loc




http://www.kler.cn/a/542664.html

相关文章:

  • 基于YoloV11和驱动级鼠标模拟实现Ai自瞄
  • 数字滤波器的分类
  • 算法之 博弈问题
  • [前端]CRX持久化
  • 基于html写一个音乐动态爱心盒子有音乐和导航基本功能实现
  • 《LeetCode Hot100》 Day01
  • 2024.1.2版本Android Studio gradle下载超时问题处理
  • python基础入门:6.2JSON与CSV数据处理
  • SkyWalking 10.1.0 实战:从零构建全链路监控,解锁微服务性能优化新境界
  • qt QCommandLineOption 详解
  • 蓝桥杯算法日记|2.11二分算法
  • 【目标检测xml2json】label从VOC格式xml文件转COCO格式json文件
  • PostgreSQL 数据库压力测试指南
  • 普通用户授权docker使用权限
  • docker和docker compose版本太低问题的解决方案
  • 16.React学习笔记.React更新机制
  • 大模型被偷家?CNN结合多模态!
  • 2025.2.11——一、[极客大挑战 2019]PHP wakeup绕过|备份文件|代码审计
  • 前端设计模式介绍及案例(单例模式、代理模式、工厂模式、装饰者模式、观察者模式)
  • SpringBoot 统一功能处理之拦截器、数据返回格式、异常处理
  • clone gerrit repos 到windows本地
  • 算法设计-归并排序(C++)
  • Elasticsearch:如何使用 Elastic 检测恶意浏览器扩展
  • 基于GA遗传优化的电动汽车光储充电站容量配置算法matlab仿真
  • STL(八)—— stack和queue的模拟
  • DeepAR:一种用于时间序列预测的深度学习模型