当前位置:网站首页>leetcode 哈希表类
leetcode 哈希表类
2022-07-26 19:58:00 【Yuki_1999】
题目1:两数之和
哈希表法
1、一般哈希表都是用来快速判断一个元素是否出现集合里
2、哈希法牺牲了空间换取了时间,因为要使用额外的数组,set或者是map来存放数据,才能实现快速的查找。
3、数组大小受限;set是一个集合,里面放的元素只能是一个key;map使用key value结构来存放,key来存元素,value来存下标
class Solution(object):
def twoSum(self, nums, target):
hash={
}
#找:当前num+过去num(存在哈希表里)=target
for i,num in enumerate(nums):
if target-num in hash:
return [hash[target-num],i]
else:
hash[num]=i
return -1
题目349:两个数组求交集
class Solution(object):
def intersection(self, nums1, nums2):
return list(set(nums1)&set(nums2))
#两个集合求交集
题目242:有效的字母异位词
class Solution(object):
def isAnagram(self, s, t):
records=[0]*26 #建立26字母的字典
#ord ()函数用于返回一个字符的ASCII值
#不需要记住ASCII码,只要求一个相对数值
for i in range(len(s)):
records[ord(s[i])-ord("a")]+=1
for i in range(len(t)):
records[ord(t[i])-ord("a")]-=1
for i in records:
if i!=0:
return False
break
return True
边栏推荐
- BUU刷题记2
- Group convolution
- 从零开始搭建Prometheus自动监控报警系统
- BUU刷题记3
- Build Prometheus automatic monitoring and alarm system from scratch
- LCP 11. Statistics of expected number
- Buu brush inscription 2
- Quick start to connection pooling
- 英国德国相继推出5G商用服务,华为成幕后功臣
- The lawyer team of the US Department of justice asked the judge to refuse to accept Huawei's lawsuit
猜你喜欢

MPLS multi protocol label switching technology

Marketing and sales document management and workflow solutions

NVIDIA canvas first experience~

深度可分离卷积(DepthwiseSeparableConvolution):Depthwise卷积与Pointwise卷积

Centos7 about Oracle RAC 11gr2 deployment disk partition

Gartner发布最新《中国AI初创企业市场指南》,弘玑Cyclone再次被评为代表性企业

实验5 OSPF综合实验

BUU刷题记4

实验6 BGP联邦综合实验

"Enterprise management" sincere crm+ - integrated management of enterprise business processes
随机推荐
BUU刷题记1
Nmap installation and use
Houdini notes 2
查询字段较多时可以添加普通查询和高级查询两种情况
Read the four service types of kubernetes!
Numpy中ndarray的常见操作
Ape tutoring's technological hard power: let AI start from reading children's homework
【面试必刷101】动态规划1
YGG cooperates with my pet hooligan, AMGI's flagship NFT project, to enter the rabbit hole
Group convolution
[基础服务] [数据库] ClickHouse的安装和配置
Common operations of ndarray in numpy
MPLS multi protocol label switching technology
Houdini 笔记2
Arpspoof installation and use
【【实验分享】CCIE—BGP路由黑洞实验】
Software testing - development test content specification (project test template)
7.25模拟赛总结
从零开始搭建Prometheus自动监控报警系统
【PyQt5基本控件使用解析】