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

news/2025/3/4 16:35:45/

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


目录

      • 2/24 1656. 设计有序流
      • 2/25 2502. 设计内存分配器
      • 2/26 1472. 设计浏览器历史记录
      • 2/27 2296. 设计一个文本编辑器
      • 2/28 2353. 设计食物评分系统
      • 3/1 131. 分割回文串
      • 3/2 132. 分割回文串 II


2/24 1656. 设计有序流

数组保存数据 插入数据后
检查指针节点是否可以往后移动

python">class OrderedStream(object):def __init__(self, n):""":type n: int"""self.n = nself.ptr = 0self.l = [""]*(n+1)def insert(self, idKey, value):""":type idKey: int:type value: str:rtype: List[str]"""ans = []self.l[idKey] = valuewhile self.ptr<self.n and self.l[self.ptr+1]!="":ans.append(self.l[self.ptr+1])self.ptr +=1if self.ptr>self.n:breakreturn ans

2/25 2502. 设计内存分配器

使用长度为n的数字 记录每个位置内存使用情况

python">class Allocator(object):def __init__(self, n):""":type n: int"""self.n=nself.mem=[0]*ndef allocate(self, size, mID):""":type size: int:type mID: int:rtype: int"""cnt = 0for i in range(self.n):if self.mem[i]>0:cnt=0else:cnt+=1if cnt==size:for j in range(i-cnt+1,i+1):self.mem[j]=mIDreturn i-cnt+1return -1def freeMemory(self, mID):""":type mID: int:rtype: int"""cnt = 0for i in range(self.n):if self.mem[i]==mID:cnt+=1self.mem[i]=0return cnt

2/26 1472. 设计浏览器历史记录

模拟 使用一个数组urls保存网页记录
cur指向当前访问的网页索引

python">class BrowserHistory(object):def __init__(self, homepage):""":type homepage: str"""self.urls = [homepage]self.cur = 0def visit(self, url):""":type url: str:rtype: None"""self.urls = self.urls[:self.cur+1]self.urls.append(url)self.cur+=1def back(self, steps):""":type steps: int:rtype: str"""self.cur = max(0,self.cur-steps)return self.urls[self.cur]def forward(self, steps):""":type steps: int:rtype: str"""self.cur = min(len(self.urls)-1,self.cur+steps)return self.urls[self.cur]

2/27 2296. 设计一个文本编辑器

使用left,right两个栈分别存储光标左右的内容
add:将text压入left中
delete:从left中取出k个直至为空
cursorleft:将left中取出放入right中
cursorright:将right中取出放入left中

python">class TextEditor(object):def __init__(self):self.left=[]self.right=[]def addText(self, text):""":type text: str:rtype: None"""self.left.extend(text)def deleteText(self, k):""":type k: int:rtype: int"""cnt=min(k,len(self.left))del self.left[-cnt:]return cntdef cursorLeft(self, k):""":type k: int:rtype: str"""for _ in range(min(k,len(self.left))):self.right.append(self.left.pop())return ''.join(self.left[-10:])def cursorRight(self, k):""":type k: int:rtype: str"""for _ in range(min(k,len(self.right))):self.left.append(self.right.pop())return ''.join(self.left[-10:])

2/28 2353. 设计食物评分系统

foodmap维护food对应的分数和烹饪方法
ratemap[cuisines] 使用最小堆维护同一烹饪方法中的分数 用负数变为最大堆

python">import heapq
class FoodRatings(object):def __init__(self, foods, cuisines, ratings):""":type foods: List[str]:type cuisines: List[str]:type ratings: List[int]"""self.food={}self.rate={}self.n=len(foods)for i in range(self.n):f = foods[i]c = cuisines[i]r = ratings[i]self.food[f]=(c,r)if c not in self.rate:self.rate[c]=[]heapq.heappush(self.rate[c], (-r,f))def changeRating(self, food, newRating):""":type food: str:type newRating: int:rtype: None"""c,r = self.food[food]heapq.heappush(self.rate[c], (-newRating,food))self.food[food]=(c,newRating)def highestRated(self, cuisine):""":type cuisine: str:rtype: str"""while self.rate[cuisine]:r,f = self.rate[cuisine][0]if -r == self.food[f][1]:return fheapq.heappop(self.rate[cuisine])return ""

3/1 131. 分割回文串

check检查字符串l是否回文
pdic存放pos开头所有回文的子串

python">def partition(s):""":type s: str:rtype: List[List[str]]"""def check(l):t = Trueif len(l)==0:return twhile t and len(l)>1:if l[0]==l[-1]:l.pop(0)l.pop(-1)else:t = Falseif len(l)>1:return Falseelse:return Trueret =[]if len(s)==0:return retl = list(s)    dic = {}for i in range(len(l)):begin = l[i]for j in range(len(l)-1,i-1,-1):if begin == l[j]:if check(l[i:j+1]):dic[(i,j)] = l[i:j+1]pdic = {}for i in dic:pos = i[0]tmp = pdic.get(pos,[])tmp.append(i)pdic[pos] = tmpdef combine(begin):ret = []va = pdic[begin]for v in va:end = v[1]if end == (len(s)-1):ret.append([v])else:l = combine(end+1)for t in l:t.insert(0,v)ret.extend(l)     return retr = combine(0)for v in r:tmp = []for i in v:tmp.append(''.join(dic[i]))ret.append(tmp)return ret

3/2 132. 分割回文串 II

m[i][j]true表示[i,j]为回文串
dp[i]记录0~i最少分割切几次

python">
def minCut(s):""":type s: str:rtype: int"""n = len(s)##预处理 判断i,j是否为回文串m = [[True]*n for _ in range(n)] for i in range(n-1,-1,-1):for j in range(i+1,n):m[i][j] = (s[i]==s[j]) and m[i+1][j-1]dp = [float("inf")] * nfor i in range(n):if m[0][i]: ##如果0-i为回文串 不需要切割dp[i]=0else:for j in range(i):if m[j+1][i]:dp[i] = min(dp[i],dp[j]+1)return dp[n-1]


http://www.ppmy.cn/news/1576585.html

相关文章

Spring Cloud — 消息驱动 Stream

Spring Cloud Stream 是让微服务更容易在应用中实现消息的发布和订阅处理的框架。Stream 支持与多种消息中间件整合&#xff0c;如Kafka、RibbitMQ等。 本文使用的是Kafka消息中间件&#xff0c;依赖文件为&#xff1a; <dependency><groupId>org.springframewor…

Powershell和BTEQ工具实现带多组参数和标签的Teradata数据库批量数据导出程序

设计一个基于多个带标签SQL模板作为配置文件和多组参数的Powershell代码程序和BTEQ工具&#xff0c;实现根据不同的输入参数&#xff0c;自动批量地将Teradata数据库的数据导出为CSV文件到指定目录上&#xff0c;标签和多个参数&#xff08;以“_”分割&#xff09;为组成导出数…

摄像头应用编程(四):ARM Linux LCD实时预览UVC摄像头画面

文章目录 1、前言2、环境介绍3、步骤4、应用程序编写4.1、lcd初始化4.2、摄像头初始化4.3、jpeg解码4.4、开启摄像头4.5、完整的程序如下 5、测试5.1、编译应用程序5.2、运行应用程序 6、总结 1、前言 本次应用程序主要针对支持MJPEG格式输出的UVC摄像头。 2、环境介绍 rk35…

GPT-4.5震撼登场,AI世界再掀波澜!(3)

GPT-4.5震撼登场&#xff0c;AI世界再掀波澜! GPT-4.5震撼登场&#xff0c;AI世界再掀波澜!(2) &#xff08;一&#xff09;伦理困境&#xff1a;如何抉择 GPT-4.5 的强大功能在为我们带来诸多便利的同时&#xff0c;也引发了一系列深刻的伦理问题&#xff0c;这些问题犹如高…

【pytest框架源码分析四】pluggy源码分析之hook执行

pluggy的主要执行方法在_callers.py中&#xff0c;这里简单介绍下。 def _multicall(hook_name: str,hook_impls: Sequence[HookImpl],caller_kwargs: Mapping[str, object],firstresult: bool, ) -> object | list[object]:"""Execute a call into multipl…

Unity 内置渲染管线各个Shader的用途和性能分析,以及如何修改Shader(build in shader 源码下载)

文章目录 所有Shader分析路径&#xff1a;Standard路径&#xff1a;Nature/路径&#xff1a;UI/路径&#xff1a;Particles/Particles/Standard SurfaceParticles/Standard Unlit 路径&#xff1a;Unlit/Unlit/TextureUnlit/ColorUnlit/TransparentUnlit/Transparent CutoutUnl…

android12 屏幕亮度控制修改为线性变化

由于高版本的亮度调节不是线性变化了,有客户反馈在Android11或者12上使用代码获取亮度不对,比如我们在设置中查看屏幕亮度是80%,读出来的亮度值是100,客户认为亮度值是39%。 获取屏幕亮度adb shell settings get system screen_brightness 或者 adb shell cat /sys/class…

STM32G431RBT6——(2)浅析Cortex-M4内核

本篇博客是一个对Cortex-M4内核了解性的简介&#xff0c;不会涉及到深奥的理论&#xff0c;请大家放心食用。 我们所学习的STM32G431RBT6单片机是基于ARM的Cotex-M4内核&#xff0c;因此我们有必要对此内核做一个大概了解。其实M4内核和M3内核有很大的相似之处&#xff0c;很多…