当前位置:网站首页>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刷题记4
- Experiment 5 OSPF comprehensive experiment
- How to implement an asynchronous task queue system that can handle massive data (supreme Collection Edition)
- 软件测试-开发提测内容规范(项目提测模板)
- LCP 11. Statistics of expected number
- 美司法部律师团队要求法官拒绝受理华为诉讼
- Pspice simulation quartz crystal oscillation circuit
- Task 1 report
- 884. Uncommon words in two sentences - hash table
- BUU刷题记1
猜你喜欢

The typing competition is over!

Group convolution

ST表、带权并查集

Buu brush inscription 3

Game partner topic: breederdao and ultiverse have established a new metauniverse

Marketing and sales document management and workflow solutions

Chat software project development 2

查询字段较多时可以添加普通查询和高级查询两种情况

TinUI发展历程

St table, weighted and search set
随机推荐
Can the training software test be employed
Gartner released the latest market guide for Chinese AI start-ups, and Hongji cyclone was once again rated as a representative enterprise
【面试必刷101】动态规划1
[基础服务] [数据库] ClickHouse的安装和配置
Gbase learning - install gbase 8A MPP cluster v95
Opencv DNN deployment onnx model
Depthwiseseparableconvolution: depthwise convolution and pointwise convolution
tkinter使用wpf控件
Where are the single dogs in the evening of 5.20?
解决IBGP的水平分割和BGP选路原则
Hello, how are you
The UK and Germany have successively launched 5g commercial services, and Huawei has become a behind the scenes hero
如何组装一个注册中心?
[experiment sharing] CCIE BGP routing black hole experiment]
Gartner发布最新《中国AI初创企业市场指南》,弘玑Cyclone再次被评为代表性企业
英国德国相继推出5G商用服务,华为成幕后功臣
易基因|宏病毒组测序技术介绍
Read the four service types of kubernetes!
BGP routing black hole and anti ring
【Delphi】FMX Form的BorderStyles不同平台说明