記錄了初步解題思路 以及本地實現代碼;并不一定為最優 也希望大家能一起探討 一起進步
目錄
- 2/24 1656. 設計有序流
- 2/25 2502. 設計內存分配器
- 2/26 1472. 設計瀏覽器歷史記錄
- 2/27 2296. 設計一個文本編輯器
- 2/28 2353. 設計食物評分系統
- 3/1 131. 分割回文串
- 3/2
2/24 1656. 設計有序流
數組保存數據 插入數據后
檢查指針節點是否可以往后移動
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的數字 記錄每個位置內存使用情況
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指向當前訪問的網頁索引
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中
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] 使用最小堆維護同一烹飪方法中的分數 用負數變為最大堆
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開頭所有回文的子串
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