当前位置:网站首页>LeetCode 每日一题 2021/7/11-2021/7/17
LeetCode 每日一题 2021/7/11-2021/7/17
2022-07-17 06:14:00 【alphaTao】
记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步
目录
7/11 676. 实现一个魔法字典
将单词按照长度分组
在相同长度的单词中寻找是否满足
class MagicDictionary(object):
def __init__(self):
""" Initialize your data structure here. """
from collections import defaultdict
self.dic=defaultdict(list)
def buildDict(self, dict):
""" Build a dictionary through a list of words :type dict: List[str] :rtype: None """
for w in dict:
num = len(w)
self.dic[num].append(w)
def search(self, word):
""" Returns if there is any word in the trie that equals to the given word after modifying exactly one character :type word: str :rtype: bool """
num = len(word)
if num not in self.dic.keys():
return False
for w in self.dic[num]:
tag = 0
for i in range(num):
if w[i]!=word[i]:
tag+=1
if tag==1:
return True
return False
7/12 1252. 奇数值单元格的数目
用两个数组分别记录行、列被添加的次数
最后遍历每个格子在行+列的总次数是否奇数个
def oddCells(m, n, indices):
""" :type m: int :type n: int :type indices: List[List[int]] :rtype: int """
row,col = [0]*m,[0]*n
for i,j in indices:
row[i]+=1
col[j]+=1
ans = 0
for i in range(m):
for j in range(n):
if (row[i]+col[j])%2==1:
ans +=1
return ans
7/13 735. 行星碰撞
从头开始考虑
ans存放当前已处理行星
如果ans[-1]为负方向 则当前这个可以直接放入
否则 如果当前为正也可以放入
如果当前为负 则从尾开始比较
def asteroidCollision(asteroids):
""" :type asteroids: List[int] :rtype: List[int] """
ans = [asteroids[0]]
for v in asteroids[1:]:
if ans[-1]<0 or v>0:
ans.append(v)
continue
tag = True
while ans[-1]>0:
if ans[-1]>-v:
tag = False
break
elif ans[-1]==-v:
tag = False
ans.pop(-1)
break
else:
ans.pop(-1)
if tag:
ans.append(v)
return ans
7/14 745. 前缀和后缀搜索
两个字典树 分别匹配
class WordFilter(object):
def __init__(self, words):
""" :type words: List[str] """
pre,suf = {
},{
}
m={
}
for i,word in enumerate(words):
m[word] = i
for word in m:
i=m[word]
tmp = pre
for c in word:
if c in tmp:
tmp = tmp[c]
tmp["ind"].add(i)
else:
tmp[c]={
}
tmp = tmp[c]
tmp["ind"] = set([i])
tmp = suf
for c in word[::-1]:
if c in tmp:
tmp = tmp[c]
tmp["ind"].add(i)
else:
tmp[c]={
}
tmp = tmp[c]
tmp["ind"] = set([i])
self.pre = pre
self.suf = suf
self.mem={
}
def f(self, pref, suff):
""" :type pref: str :type suff: str :rtype: int """
memtag = pref+","+suff
if memtag in self.mem:
return self.mem[memtag]
tmp = self.pre
tag = True
for c in pref:
if c in tmp:
tmp = tmp[c]
else:
tag = False
break
if not tag:
self.mem[memtag] = -1
return -1
prenum = tmp["ind"]
tmp = self.suf
tag = True
for c in suff[::-1]:
if c in tmp:
tmp = tmp[c]
else:
tag = False
break
if not tag:
self.mem[memtag] = -1
return -1
sufnum = tmp["ind"]
l = list(prenum&sufnum)
ans = -1
if l:
ans = max(l)
self.mem[memtag] = ans
return ans
7/15 558. 四叉树交集
dfs
如果两个节点有一个为叶子节点
如果叶子节点为true 则结果为这个叶子节点 否则为另一个叶子节点
如果两个节点均不为叶子节点
分别考虑四个子部分 如果四个部分相同 则合并
否则不合并
class Node(object):
def __init__(self, val, isLeaf, topLeft, topRight, bottomLeft, bottomRight):
self.val = val
self.isLeaf = isLeaf
self.topLeft = topLeft
self.topRight = topRight
self.bottomLeft = bottomLeft
self.bottomRight = bottomRight
def intersect(quadTree1, quadTree2):
""" :type quadTree1: Node :type quadTree2: Node :rtype: Node """
def check(node1,node2):
if node1.isLeaf:
if node1.val:
return node1
else:
return node2
if node2.isLeaf:
if node2.val:
return node2
else:
return node1
p1 = check(node1.topLeft,node2.topLeft)
p2 = check(node1.topRight,node2.topRight)
p3 = check(node1.bottomLeft,node2.bottomLeft)
p4 = check(node1.bottomRight,node2.bottomRight)
if p1.isLeaf and p2.isLeaf and p3.isLeaf and p4.isLeaf and p1.val==p2.val==p3.val==p4.val:
return Node(p1.val,True)
return Node(False,False,p1,p2,p3,p4)
return check(quadTree1, quadTree2)
7/16 剑指 Offer II 041. 滑动窗口的平均值
模拟滑动窗口 记录总和
class MovingAverage(object):
def __init__(self, size):
""" Initialize your data structure here. :type size: int """
self.l = []
self.n = size
self.sum = 0
def next(self, val):
""" :type val: int :rtype: float """
self.l.append(val)
self.sum += val
if len(self.l)>self.n:
v = self.l.pop(0)
self.sum -= v
return self.sum*1.0/len(self.l)
7/17
边栏推荐
- Xinlinx zynq7010 domestic replacement fmql10s400 national production arm core board + expansion board
- @ConditionalOnMissingBean 如何实现覆盖第三方组件中的 Bean
- RISC-V技术杂谈
- [JVM] heap memory, escape analysis, on stack allocation, synchronous omission, scalar replacement details
- 理解LSTM和GRU
- VMware Cloud Director 10.4 发布 (含下载) - 云计算调配和管理平台
- YOLOV5-打标签建立自己的数据集
- 《牛客刷题》sql错题集
- CCF-CSP《202206-2—寻宝!大冒险!》
- xgboos-hperopt
猜你喜欢

半导体材料技术

VMware cloud director 10.4 release (including download) - cloud computing provisioning and management platform

pytorch随记(5)

175. Combine two tables (MySQL database connection)

Modify checkbox style

4-channel fmc+ baseband signal processing board (4-channel 2G instantaneous bandwidth ad+da)

js数组交集、差集和并集
![[MySQL] lock mechanism: detailed explanation of lock classification, table lock, row lock and page lock in InnoDB engine](/img/7e/ddf05e76da26e9b2d1fd1519703571.png)
[MySQL] lock mechanism: detailed explanation of lock classification, table lock, row lock and page lock in InnoDB engine

59 get prototype size

Pytoch notes (3)
随机推荐
Pytorch随记(2)
[MySQL] mvcc: correctly understand mvcc and its implementation principle
[MySQL] transaction: basic knowledge of transaction, implementation principle of MySQL transaction, detailed explanation of transaction log redolog & undo
力扣114题:二叉树展开链表
Pytoch notes (2)
Convolutional neural network CNN
@ConditionalOnMissingBean 如何实现覆盖第三方组件中的 Bean
59 get prototype size
收单外包服务商北京捷文科技以约4.8亿转让60%股份
How does Jenkins set the mailbox to automatically send mail?
Pytorch随记(1)
泰坦尼克号乘客获救预测(进阶)
How to write the highlights of SCI papers (seriously teach you how to write)
redis 存储结构原理 2
175. Combine two tables (MySQL database connection)
Export file or download file
京东购买意向预测(四)
【MySQL】 MVCC:正确理解MVCC及其实现原理
Flutter3.0(framework框架)——UI渲染
企业微信上的应用如何与门店的sybase数据库关联