欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 培训 > LeetCode 每日一题 2025/1/6-2025/1/12

LeetCode 每日一题 2025/1/6-2025/1/12

2025/5/18 9:28:20 来源:https://blog.csdn.net/zkt286468541/article/details/145033195  浏览:    关键词:LeetCode 每日一题 2025/1/6-2025/1/12

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


目录

      • 1/6 2274. 不含特殊楼层的最大连续楼层数
      • 1/7 3019. 按键变更的次数
      • 1/8 2264. 字符串中最大的 3 位相同数字
      • 1/9 3297. 统计重新排列后包含另一个字符串的子字符串数目 I
      • 1/10 3298. 统计重新排列后包含另一个字符串的子字符串数目 II
      • 1/11 3270. 求出数字答案
      • 1/12 2275. 按位与结果大于零的最长组合


1/6 2274. 不含特殊楼层的最大连续楼层数

将bottom top加入数组中 遍历算出相邻最大值

def maxConsecutive(bottom, top, special):""":type bottom: int:type top: int:type special: List[int]:rtype: int"""special.sort()l = [bottom-1]+special+[top+1]return max([l[i+1]-l[i]-1 for i in range(len(l)-1)] )

1/7 3019. 按键变更的次数

变成小写字母
从头遍历 寻找相邻不同的个数

def countKeyChanges(s):""":type s: str:rtype: int"""s=s.lower()ans = 0for i in range(1,len(s)):if s[i]!=s[i-1]:ans+=1return ans

1/8 2264. 字符串中最大的 3 位相同数字

遍历 判断是否存在连续3个相同数字

def largestGoodInteger(num):""":type num: str:rtype: str"""v=""for i in range(len(num)-2):if num[i]==num[i+1]==num[i+2]:if num[i]>v:v=num[i]return "" if v=="" else v*3

1/9 3297. 统计重新排列后包含另一个字符串的子字符串数目 I

满足条件的x 即x内需要包含word2所有字符
对于每个子字符串左侧端点l 找到它满足条件最短时的右端点r
更右侧的所有必定都满足 可以有n-r+1个子字符串
滑动窗口记录l,r
diff记录满足word2每个字符还差几个
cnt记录不满足个数的字符个数

def validSubstringCount(word1, word2):""":type word1: str:type word2: str:rtype: int"""n=len(word1)diff=[0]*26for c in word2:diff[ord(c)-ord('a')]-=1ans = 0global cntcnt = sum(1 for c in diff if c<0)def update(c,add):global cntdiff[c]+=addif add==1 and diff[c]==0:cnt-=1elif add==-1 and diff[c]==-1:cnt+=1l,r=0,0while l<len(word1):while r<len(word1) and cnt>0:update(ord(word1[r])-ord('a'),1)r+=1 if cnt==0:ans+=n-r+1update(ord(word1[l])-ord('a'), -1)l+=1 return ans

1/10 3298. 统计重新排列后包含另一个字符串的子字符串数目 II

满足条件的x 即x内需要包含word2所有字符
对于每个子字符串左侧端点l 找到它满足条件最短时的右端点r
更右侧的所有必定都满足 可以有n-r+1个子字符串
滑动窗口记录l,r
diff记录满足word2每个字符还差几个
cnt记录不满足个数的字符个数

def validSubstringCount(word1, word2):""":type word1: str:type word2: str:rtype: int"""n=len(word1)diff=[0]*26for c in word2:diff[ord(c)-ord('a')]-=1ans = 0global cntcnt = sum(1 for c in diff if c<0)def update(c,add):global cntdiff[c]+=addif add==1 and diff[c]==0:cnt-=1elif add==-1 and diff[c]==-1:cnt+=1l,r=0,0while l<len(word1):while r<len(word1) and cnt>0:update(ord(word1[r])-ord('a'),1)r+=1 if cnt==0:ans+=n-r+1update(ord(word1[l])-ord('a'), -1)l+=1 return ans

1/11 3270. 求出数字答案

依次求最小值

def generateKey(num1, num2, num3):""":type num1: int:type num2: int:type num3: int:rtype: int"""ans = 0for i in range(4):ans+=min(num1%10,num2%10,num3%10)*(10**i)num1//=10num2//=10num3//=10return ans

1/12 2275. 按位与结果大于零的最长组合

计算每一位为1的数个数 这些数相与必定大于0
求个数最大值即可

def largestCombination(candidates):""":type candidates: List[int]:rtype: int"""def find(i):ans = 0for num in candidates:if num&(1<<i):ans+=1return ansans = 0for i in range(24):ans = max(ans,find(i))return ans

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词