面试之哈希表leetcode
1 案例1 leetcode-----242
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
示例 1:
输入: s = "anagram", t = "nagaram"
输出: true
python版本
方法1:直接排序
方法二:API map计数
方法三:数组模拟hash
'''
方法1 按照字母序排序 如果一样则是 时间复杂度是nlogN 快排 方法2 数字符串每个字符串字符的个数,也就是使用map来计算{letter:count}
a---->
n---->
r---->
时间复杂度O(N) * 为O(N)
''' class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
#方法一
#return sorted(s)==sorted(t) #方法二
'''
dic1,dic2={},{}
for item in s:
dic1[item]=dic1.get(item,)+
for item in t:
dic2[item]=dic2.get(item,)+
return dic1==dic2
'''
#方法三
dic1,dic2=[]*,[]*
for item in s:
dic1[ord(item)-ord('a')]+=
for item in t:
dic2[ord(item)-ord('a')]+=
return dic1==dic2
2 案例2 leetcode------1
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
示例:
给定 nums = [2, 7, 11, 15], target = 9
因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int] dict1={}
for i in range(,len(nums)):
num=target-nums[i]
if num not in dict1:
dict1[nums[i]]=i
else:
return [dict1[num],i]
"""
hash_map=dict()
for i,x in enumerate(nums):
if target-x in hash_map:
return [i,hash_map[target-x]];
hash_map[x]=i
3 案例3 leetcode---15
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
方法1:暴力求解 三层循环 时间复杂度n*n*n
方法2:c= -(a+b)枚举ab两层循环,然后在set中查询-(a+b) 时间复杂度n*n 多了空间复杂度n
方法3:sort find: 整个数组排序o(n*logn),首先先第一层循环取出第一个元素a,然后后面使用二分查找bc两个值,如果存在b+c=a则找到,时间复杂度O(N*N)
python版本:
class Solution(object):
def threeSum(self, nums):
nums.sort()#先排序
n = len(nums)
res = []
#print(nums)
for i in range(n):
if i > and nums[i] == nums[i-]:
continue
#左右边界
left = i +
right = n -
while left < right:
cur_sum = nums[i] + nums[left] + nums[right]
if cur_sum == :
tmp = [nums[i],nums[left],nums[right]]
res.append(tmp)
#去掉重复的解
while left < right and nums[left] == nums[left+]:
left +=
while left < right and nums[right] == nums[right-]:
right -=
left +=
right -=
elif cur_sum > :
right -=
else:
left +=
return res
c++版本
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
int target;
vector<vector<int>> ans;
sort(nums.begin(), nums.end());
for (int i = ; i < nums.size(); i++) {
if (i > && nums[i] == nums[i - ]) continue;
if ((target = nums[i]) > ) break;
int l = i + , r = nums.size() - ;
while (l < r) {
if (nums[l] + nums[r] + target < ) ++l;
else if (nums[l] + nums[r] + target > ) --r;
else {
ans.push_back({target, nums[l], nums[r]});
++l, --r;
while (l < r && nums[l] == nums[l - ]) ++l;
while (l < r && nums[r] == nums[r + ]) --r;
}
}
}
return ans;
}
};
面试之哈希表leetcode的更多相关文章
- 拼写单词[哈希表]----leetcode周赛150_1001
题目描述: 给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars. 假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我 ...
- 重复的DNA序列[哈希表] LeetCode.187
所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助. 编写一个函数 ...
- 无重复字符的最长子串[双指针+哈希表] LeetCode.3
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc&qu ...
- python code practice(二):KMP算法、二分搜索的实现、哈希表
1.替换空格 题目描述:请实现一个函数,将一个字符串中的每个空格替换成“%20”.例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 分析: 将长度为 ...
- LeetCode通关:哈希表六连,这个还真有点简单
精品刷题路线参考: https://github.com/youngyangyang04/leetcode-master https://github.com/chefyuan/algorithm-b ...
- [LeetCode] #1# Two Sum : 数组/哈希表/二分查找/双指针
一. 题目 1. Two SumTotal Accepted: 241484 Total Submissions: 1005339 Difficulty: Easy Given an array of ...
- LeetCode刷题总结-哈希表篇
本文总结在LeetCode上有关哈希表的算法题,推荐刷题总数为12题.具体考察的知识点如下图: 1.数学问题 题号:149. 直线上最多的点数,难度困难 题号:554. 砖墙,难度中等(最大最小边界问 ...
- Leetcode Lect7 哈希表
传统的哈希表 对于长度为n的哈希表,它的存储过程如下: 根据 key 计算出它的哈希值 h=hash(key) 假设箱子的个数为 n,那么这个键值对应该放在第 (h % n) 个箱子中 如果该箱子中已 ...
- C#LeetCode刷题-哈希表
哈希表篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 42.8% 简单 3 无重复字符的最长子串 24.2% 中等 18 四数之和 ...
随机推荐
- python通过LXML库读取xml命名空间
xml实例版本: <a> <city:table xmlns:city="city"> <heilongjiang name="citys& ...
- shortcuts for contructor 创建对象捷径
- java项目部署到LIINUX
天领导给个任务,把java项目部署到liunx服务器上.现记录步骤,方便以后查看.项目部署服务器步骤:服务器信息:弹性IP地址:xx.xx.xxx.xx账号:root密码:cjw@100 数据库信息: ...
- 使用SpringBoot访问jsp页面
1 编写application.yml文件 spring: mvc: view: suffix: .jsp prefix: /jsp/ 2 创建Controller层 @Controller @Req ...
- shell随机数比较
#!/bin/bash a=$(expr $RANDOM % ) #生成一到一百的随机数 echo $a #打印随机数 b= while true do let b++ echo "比较了第 ...
- 阿里云部署自己的web服务器
阿里云部署自己的web服务器 [外链图片转存失败(img-GIKNTPPx-1564287221547)(https://upload-images.jianshu.io/upload_images/ ...
- GoCN每日新闻(2019-10-05)
国庆专辑:GopherChina祝大家国庆节快乐GoCN每日新闻(2019-10-05) 1. Gophercon UK 2019 https://www.bilibili.com/video/av ...
- mysql 选择所有同学名字
mysql> select * from test; +----+----------+-------+-----------+ | id | name | score | subject | ...
- 安装与配置HSDIS与JITWatch
本作者的系统: 操作系统版本及位数可通过uname -a命令查看,如下: Linux ubuntu 3.13.0-32-generic #57~precise1-Ubuntu SMP Tue Jul ...
- 各种系统性能优化技术,采用vilocity实现商品页面静态化
1.大型门户网站系统:>10万的访问量 行业网站(当当网,卓越网):20万-30万,一个小时内会跟数据库的交互至少20万-30万,会产生数据库瓶颈,每个数据库都有一个最大连接数(socket ...