面试之哈希表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 四数之和 ...
随机推荐
- js Date对象和数字对象
<script type="text/javascript"> alert(new Date.toLocaleString()); </script> 以本 ...
- vs下载代码
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u012605477/article/details/62222919Visual Studio 20 ...
- robot framework中如何为每个测试用例,测试集准备数据或销毁数据
Suite Setup:在这个测试集的所有测试用例开始测试之前运行(类似于junit的@BeforeClass) Suite Teardown:在这个测试集的所有测试用例结束之后运行(类似于junit ...
- CentOS7下安装Python3和PIP3
为了方便以后编译,所以整合了下配置流程 先查看是否安装了Python3 python3 -V 如果没有安装,则先安装依赖包 yum install zlib-devel bzip2-devel ope ...
- svn服务的安装与设置 .
1. 下载svn软件并安装,本人使用的是如下软件: TortoiseSVN-1.6.5.16974-win32-svn-1.6.5 Vis ...
- zzulioj - 2628: 小新的字母广场
题目链接:http://acm.zzuli.edu.cn/problem.php?id=2628 题目描述 放假了,小新决定出去散散心,于是他来到了著名的字母广场.这个广场是由n*m块砖 ...
- H5如何实现关闭当前页面,跳转到新页面?
小程序有此功能的跳转方法. 那么H5如何实现该功能? 很简单. location.replace('new.html') 这个方法可以实现 关闭当前页面,跳转到新页面 的效果. 而 windo ...
- 从TEB到PEB再到SEH(二)
什么是SEH? SEH( Structured Exception Handling , 结构化异常处理 ) 结构化异常处理(SEH)是Windows操作系统提供的强大异常处理功能.而Visual C ...
- 文件搜索命令find
1.路径加文件名搜索(find): 查找的是etc目录下的以init为名字的文件. 加通配符后为模糊搜索,只要文件名中含有init即可. 查找etc目录下以init开头的七位文件名. 2.搜索时不区分 ...
- Nodejs中的路径问题
一.path核心模块 ①path.basename(path[,ext])获取一个路径中的文件名 var path=require('path'); console.log(path.basename ...