当前位置:网站首页>哈希表卡片
哈希表卡片
2022-08-09 13:10:00 【花椒酱不吃花椒喵】
dict的各种函数
dict.get(key,defaultvalue)
class Solution(object):
def intersect(self, nums1, nums2):
counts = {
}
res = []
for num in nums1:
counts[num] = counts.get(num, 0) + 1
for num in nums2:
if num in counts and counts[num] > 0:
res.append(num)
counts[num] -= 1
return res
其他函数
enumerate遍历list的索引和值
zip同时遍历list或dict
e.g.:zip(dict1.values(),dict2.values())
s.find(x)找到数组s中值为x的索引,s.rfind(x)在数组中找到最后一个值为x的索引
class Solution:
def findRestaurant(self, list1: List[str], list2: List[str]) -> List[str]:
Aindex = {
u: i for i, u in enumerate(list1)}
best, ans = 1e9, []
for j, v in enumerate(list2):
i = Aindex.get(v, 1e9)
if i + j < best:
best = i + j
ans = [v]
elif i + j == best:
ans.append(v)
return ans
边栏推荐
猜你喜欢
随机推荐
记录本项目中用到的系统调用与C库函数-2
pytest 与 unittest 的区别
NC7 买卖股票的最好时机(一)
对百度的内容进行修改
RobotFramework 之 数据驱动
Sandbox中的进程/线程相关-1
禁止输入(×),按键精灵小程序,快速上手
NC84 完全二叉树结点数
FFMPEG multimedia file processing (deletion and renaming of ffmpeg files)
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面(循环不变量)
FFmpeg av_interleaved_write_frame错误
Deep Learning Based on R Language - Simple Regression Case
pyautogui的简单操作(1)
RobotFramework 之 条件判断
RTP打包发送H.264
行程和用户[阅读理解法]
CutefishOS系统默认自动桌面壁纸
puzzle(016.5)逻辑电路
pytest 筛选用例
javscript基础易错点集合