LeetCode 每日一题 2025/3/10-2025/3/16

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


目录

      • [3/10 2269. 找到一个数字的 K 美丽值](#3/10 2269. 找到一个数字的 K 美丽值)
      • [3/11 2012. 数组美丽值求和](#3/11 2012. 数组美丽值求和)
      • [3/12 3305. 元音辅音字符串计数 I](#3/12 3305. 元音辅音字符串计数 I)
      • [3/13 3306. 元音辅音字符串计数 II](#3/13 3306. 元音辅音字符串计数 II)
      • [3/14 3340. 检查平衡字符串](#3/14 3340. 检查平衡字符串)
      • [3/15 3110. 字符串的分数](#3/15 3110. 字符串的分数)
      • [3/16 2272. 最大波动的子字符串](#3/16 2272. 最大波动的子字符串)

3/10 2269. 找到一个数字的 K 美丽值

依次判断

python 复制代码
def divisorSubstrings(num, k):
    """
    :type num: int
    :type k: int
    :rtype: int
    """
    n=len(str(num))
    cur = num
    MOD = 10**k
    ans=0
    for i in range(n-k+1):
        v = cur%MOD
        if v!=0 and num%v==0:
            ans+=1
        cur//=10
    return ans

3/11 2012. 数组美丽值求和

maxl[i]记录从0~i的最大值 minr[i]记录从i~n的最小值

对于nums[x] 如果maxl[x-1]<nums[x]<minr[x+1]则美丽值为2

python 复制代码
def sumOfBeauties(nums):
    """
    :type nums: List[int]
    :rtype: int
    """
    n=len(nums)
    maxl=[0]*n
    minr=[0]*n
    maxl[0]=nums[0]
    minr[-1]=nums[-1]
    for i in range(1,n):
        maxl[i]=max(maxl[i-1],nums[i])
        minr[n-1-i]=min(minr[n-i],nums[n-1-i])
    ans = 0
    for i in range(1,n-1):
        if maxl[i-1]<nums[i] and nums[i]<minr[i+1]:
            ans+=2
        elif nums[i-1]<nums[i] and nums[i]<nums[i+1]:
            ans+=1
    return ans

3/12 3305. 元音辅音字符串计数 I

恰好k个 可以看作至少k+1个 减去 至少k个

双指针

tag用来记录元音字母出现次数

python 复制代码
def countOfSubstrings(word, k):
    """
    :type word: str
    :type k: int
    :rtype: int
    """
    def func(k):
        l=0
        ans = 0
        curk = 0
        tag = {}
        for w in word:
            if w in "aeiou":
                tag[w] = tag.get(w,0)+1
            else:
                curk+=1
            while len(tag)==5 and curk>=k:
                lw = word[l]
                if lw in "aeiou":
                    tag[lw]-=1
                    if tag[lw]==0:
                        del tag[lw]
                else:
                    curk-=1
                l+=1
            ans+=l
        return ans
    return func(k)-func(k+1)

3/13 3306. 元音辅音字符串计数 II

恰好k个 可以看作至少k+1个 减去 至少k个

双指针

tag用来记录元音字母出现次数

python 复制代码
def countOfSubstrings(word, k):
    """
    :type word: str
    :type k: int
    :rtype: int
    """
    def func(k):
        l=0
        ans = 0
        curk = 0
        tag = {}
        for w in word:
            if w in "aeiou":
                tag[w] = tag.get(w,0)+1
            else:
                curk+=1
            while len(tag)==5 and curk>=k:
                lw = word[l]
                if lw in "aeiou":
                    tag[lw]-=1
                    if tag[lw]==0:
                        del tag[lw]
                else:
                    curk-=1
                l+=1
            ans+=l
        return ans
    return func(k)-func(k+1)
                    

3/14 3340. 检查平衡字符串

将奇偶位置的数值相减 判断最后差值是否为0

python 复制代码
def isBalanced(num):
    """
    :type num: str
    :rtype: bool
    """
    cur=0
    n=len(num)
    for i in range(n):
        if i%2:
            cur+=int(num[i])
        else:
            cur-=int(num[i])
    return False if cur else True

3/15 3110. 字符串的分数

按要求依次计算

python 复制代码
def scoreOfString(s):
    """
    :type s: str
    :rtype: int
    """
    n=len(s)
    ans = 0
    for i in range(n-1):
        ans += abs(ord(s[i+1])-ord(s[i]))
    return ans

3/16 2272. 最大波动的子字符串

枚举子字符串中最大和最少的字符

https://leetcode.cn/problems/substring-with-largest-variance/solutions/1501524/zui-da-bo-dong-de-zi-zi-fu-chuan-by-leet-xsnp/?envType=daily-question\&envId=2025-03-16

python 复制代码
def largestVariance(s):
    """
    :type s: str
    :rtype: int
    """
    from collections import defaultdict 
    pos= defaultdict(list)
    for i,c in enumerate(s):
        pos[c].append(i)
        
    ans=0
    for c0,p0 in pos.items():
        for c1,p1 in pos.items():
            if c0!=c1:
                i=j=0
                f,g=0,float("-inf")
                while i<len(p0) or j<len(p1):
                    if j==len(p1) or (i<len(p0) and p0[i]<p1[j]):
                        f,g=max(f,0)+1,g+1
                        i+=1
                    else:
                        f,g=max(f,0)-1,max(f,g,0)-1
                        j+=1
                    ans=max(ans,g)
    return ans

相关推荐
先做个垃圾出来………37 分钟前
差分数组(Difference Array)
java·数据结构·算法
hansang_IR1 小时前
【题解】洛谷 P4286 [SHOI2008] 安全的航线 [递归分治]
c++·数学·算法·dfs·题解·向量·点积
乐迪信息1 小时前
乐迪信息:AI摄像机在智慧煤矿人员安全与行为识别中的技术应用
大数据·人工智能·算法·安全·视觉检测
多恩Stone1 小时前
【3DV 进阶-2】Hunyuan3D2.1 训练代码详细理解下-数据读取流程
人工智能·python·算法·3d·aigc
惯导马工3 小时前
【论文导读】IDOL: Inertial Deep Orientation-Estimation and Localization
深度学习·算法
老姜洛克3 小时前
自然语言处理(NLP)之n-gram从原理到实战
算法·nlp
1白天的黑夜13 小时前
哈希表-49.字母异位词分组-力扣(LeetCode)
c++·leetcode·哈希表
CoovallyAIHub3 小时前
基于YOLO集成模型的无人机多光谱风电部件缺陷检测
深度学习·算法·计算机视觉
CoovallyAIHub3 小时前
几十个像素的小目标,为何难倒无人机?LCW-YOLO让无人机小目标检测不再卡顿
深度学习·算法·计算机视觉
怀旧,3 小时前
【C++】19. 封装红⿊树实现set和map
linux·c++·算法