记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步
长度为k的数组 头尾指针fr,la
class MyCircularDeque(object):
def __init__(self, k):
"""
:type k: int
"""
self.num = 0
self.l = [-1]*k
self.k = k
def insertFront(self, value):
"""
:type value: int
:rtype: bool
"""
if self.num == self.k:
return False
if self.num == 0:
self.l[0] = value
self.fr = 0
self.la = 0
else:
self.fr = (self.fr-1)%self.k
self.l[self.fr] = value
self.num +=1
return True
def insertLast(self, value):
"""
:type value: int
:rtype: bool
"""
if self.num == self.k:
return False
if self.num == 0:
self.l[0] = value
self.fr = 0
self.la = 0
else:
self.la = (self.la+1)%self.k
self.l[self.la] = value
self.num +=1
return True
def deleteFront(self):
"""
:rtype: bool
"""
if self.num == 0:
return False
self.fr = (self.fr+1)%self.k
self.num -=1
return True
def deleteLast(self):
"""
:rtype: bool
"""
if self.num == 0:
return False
self.la = (self.la-1)%self.k
self.num -=1
return True
def getFront(self):
"""
:rtype: int
"""
if self.num == 0:
return -1
return self.l[self.fr]
def getRear(self):
"""
:rtype: int
"""
if self.num == 0:
return -1
return self.l[self.la]
def isEmpty(self):
"""
:rtype: bool
"""
return self.num==0
def isFull(self):
"""
:rtype: bool
"""
return self.num==self.k
数组保存数据 插入数据后
检查指针节点是否可以往后移动
class OrderedStream(object):
def __init__(self, n):
"""
:type n: int
"""
self.n = n
self.ptr = 0
self.l = [""]*(n+1)
def insert(self, idKey, value):
"""
:type idKey: int
:type value: str
:rtype: List[str]
"""
ans = []
self.l[idKey] = value
while self.ptr<self.n and self.l[self.ptr+1]!="":
ans.append(self.l[self.ptr+1])
self.ptr +=1
if self.ptr>self.n:
break
return ans
BFS 记录当前层的和
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def deepestLeavesSum(root):
"""
:type root: TreeNode
:rtype: int
"""
ans = 0
l = [root]
while l:
tmp = []
ans = 0
for node in l:
ans += node.val
if node.left:
tmp.append(node.left)
if node.right:
tmp.append(node.right)
l = tmp
return ans
num记录数值出现的次数
fre记录数值出现次数的频率
maxfre记录当前最大的频率
如果maxfre=1 则之后删除任意一个数都满足
如果只有一个maxfre 其他都是maxfre-1 则删除出现最多的一个
如果有一个数只出现一次 其他都是maxfre 则删除出现一次的数
def maxEqualFreq(nums):
"""
:type nums: List[int]
:rtype: int
"""
num,fre = {},{}
ans = 1
maxfre = 0
for i,v in enumerate(nums):
if v in num:
fre[num[v]]-=1
num[v] = num.get(v,0)+1
maxfre = max(maxfre,num[v])
fre[num[v]] = fre.get(num[v],0)+1
if maxfre ==1:
ans = i+1
if fre[maxfre]==1 and maxfre+fre.get(maxfre-1,0)*(maxfre-1)==i+1:
ans = i+1
if fre.get(1,0)==1 and fre[maxfre]*maxfre+1==i+1:
ans = i+1
return ans
从头开始累加 直到查询时间点
开始时间点+1 结束时间点-1
def busyStudent(startTime, endTime, queryTime):
"""
:type startTime: List[int]
:type endTime: List[int]
:type queryTime: int
:rtype: int
"""
ans = 0
for s in startTime:
if s<=queryTime:
ans +=1
for e in endTime:
if e<queryTime:
ans -=1
return ans
递归 找出当前数组中最大值及其位置loc
[:loc]为左子树
[loc+1:]为右子树
class TreeNode(object):
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def constructMaximumBinaryTree(nums):
"""
:type nums: List[int]
:rtype: TreeNode
"""
def dfs(l):
if len(l)==0:
return None
loc = -1
maxv = float("-inf")
for i,v in enumerate(l):
if v>maxv:
loc = i
maxv = v
node = TreeNode(maxv)
node.left = dfs(l[:loc])
node.right = dfs(l[loc+1:])
return node
return dfs(nums)
分割句子 检查每个单词
def isPrefixOfWord(sentence, searchWord):
"""
:type sentence: str
:type searchWord: str
:rtype: int
"""
l = sentence.split(" ")
for loc,v in enumerate(l):
if v.startswith(searchWord):
return loc+1
return -1