当前位置:网站首页>哈希表卡片
哈希表卡片
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
边栏推荐
- [极客大挑战 2019]Upload
- ArcEngine(八) 选择要素并高亮显示
- pytest 之 fixture参数化
- Q_08 更多信息
- Professor Chen Qiang's "Machine Learning and R Application" course Chapter 16 Assignment
- Uni - app - uview Swiper shuffling figure component, click on the links to jump (click to get the item after the row data, remove data operation)
- WPF 系统托盘 图标闪烁
- Q_06_04 语句和其他构造
- 富媒体在客服IM消息通信中的秒发实践
- pytest 与 unittest 的区别
猜你喜欢
七夕力扣刷不停,343. 整数拆分(剑指 Offer 14- I. 剪绳子、剑指 Offer 14- II. 剪绳子 II)
error Trailing spaces not allowed no-trailing-spaces 9:14 error Unexpected trailing comma
IDEA Gradle 常遇问题(一)
The sword refers to the offer, cuts the rope 2
客户端连接rtsp的步骤
RobotFramework 之 Setup和Teardown
蓝桥历届真题-蛇形填数
RobotFramework 之 库与关键字
面试攻略系列(四)-- 你不知道的大厂面试
pytest 基础认知
随机推荐
对百度的内容进行修改
C#使用cersharp
音频基础学习——声音的本质、术语与特性
tensorflow图片编码处理基础
【面试高频题】可逐步优化的链表高频题
opencv-matchTemplate 之使用场景为大图里面找小图
面试攻略系列(二)-- 秒杀系统
RobotFramework 之 库与关键字
Professor Chen Qiang's "Machine Learning and R Application" course Chapter 15 Homework
面试攻略系列(三)-- 高级开发工程师面试问些啥?
Explanation of RTSP protocol
FFmpeg multimedia file processing (ffmpeg prints audio and video Meta information)
The sword refers to Offer 56 - II. Number of occurrences of a number in an array II (bit operation)
NC61 两数之和
海康设备获取YV12图像-不用rtsp
JZ7 重建二叉树
vivo手机上的系统级消息推送平台的架构设计实践
The sword refers to Offer 57 - II. and is a continuous positive sequence of s (sliding window)
Final assignment of R language data analysis in a university
An Offer 21. Adjust the array in order to make odd in even the front (loop invariant)