当前位置:网站首页>哈希表的查找与插入及删除
哈希表的查找与插入及删除
2022-07-27 19:00:00 【Al_tair】
哈希表的查找与插入及删除
大家好呀,我是小笙,这周我来分享一些哈希表的算法习题
哈希表的查找与插入及删除
217. 存在重复元素
给定一个整数数组,判断是否存在重复元素
如果存在一值在数组中出现至少两次,函数返回
true。如果数组中每个元素都不相同,则返回false示例 1:
输入: [1,2,3,1] 输出: true示例 2:
输入: [1,2,3,4] 输出: false示例 3:
输入: [1,1,1,3,3,4,3,2,4,2] 输出: true
class Solution {
public boolean containsDuplicate(int[] nums) {
Arrays.sort(nums);
for(int i=0;i<nums.length-1;i++){
if(nums[i]==nums[i+1]){
return true;
}
}
return false;
}
}
349. 两个数组的交集
给定两个数组,编写一个函数来计算它们的交集。
示例 1:
输入:nums1 = [1,2,2,1], nums2 = [2,2] 输出:[2]示例 2:
输入:nums1 = [4,9,5], nums2 = [9,4,9,8,4] 输出:[9,4]说明:
- 输出结果中的每个元素一定是唯一的。
- 我们可以不考虑输出结果的顺序。
class Solution {
public int[] intersection(int[] nums1, int[] nums2) {
if(nums1.length*nums2.length == 0) return null;
HashSet<Integer> sites = new HashSet<>();
int index = 0;
int[] array = new int[nums1.length<nums2.length?nums1.length:nums2.length];
for(int i=0;i<nums1.length;i++){
for(int j=0;j<nums2.length;j++){
if(nums1[i]==nums2[j] && sites.add(nums1[i])){
array[index] = nums1[i];
index++;
}
}
}
int[] array2 = new int[index];
for(int i=0;i<index;i++){
array2[i] = array[i];
}
return array2;
}
}
202.快乐数
难度简单730
编写一个算法来判断一个数
n是不是快乐数。「快乐数」定义为:
- 对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和。
- 然后重复这个过程直到这个数变为 1,也可能是 无限循环 但始终变不到 1。
- 如果 可以变为 1,那么这个数就是快乐数。
如果
n是快乐数就返回true;不是,则返回false。示例 1:
输入:n = 19 输出:true 解释: 12 + 92 = 82 82 + 22 = 68 62 + 82 = 100 12 + 02 + 02 = 1示例 2:
输入:n = 2 输出:false提示:
1 <= n <= 231 - 1
class Solution {
public boolean isHappy(int n) {
Set<Integer> set = new HashSet<>();
while(n != 1){
n = happyTurn(n);
if(n == 1) return true;
if(!set.add(n)) return false;
}
return true;
}
public int happyTurn(int n){
int round,sum=0; // 取余 求和
while(n > 0){
round = n%10;
n = n/10;
sum += round*round;
}
return sum;
}
}
290.单词规律
难度简单403
给定一种规律
pattern和一个字符串str,判断str是否遵循相同的规律。这里的 遵循 指完全匹配,例如,
pattern里的每个字母和字符串str中的每个非空单词之间存在着双向连接的对应规律。示例1:
输入: pattern = "abba", str = "dog cat cat dog" 输出: true示例 2:
输入:pattern = "abba", str = "dog cat cat fish" 输出: false示例 3:
输入: pattern = "aaaa", str = "dog cat cat dog" 输出: false示例 4:
输入: pattern = "abba", str = "dog dog dog dog" 输出: false说明:
你可以假设pattern只包含小写字母,str包含了由单个空格分隔的小写字母。
class Solution {
public boolean wordPattern(String pattern, String s) {
HashMap<String,Character> StrChar = new HashMap<>();
HashMap<Character,String> CharStr = new HashMap<>();
int n = s.length(),m=pattern.length(),sIndex=0,patternIndex=0;
while(--m >=0){
char ch = pattern.charAt(patternIndex++);
String str = "";
if(sIndex>=n) break;
for(int i=sIndex;i<n && s.charAt(i)!=' ';i++){
str += s.charAt(i)+"";
sIndex++;
}
sIndex+=1;
if(StrChar.containsKey(str)&&StrChar.get(str)!=ch){
return false;
}
if(CharStr.containsKey(ch)&&!CharStr.get(ch).equals(str)){
return false;
}
StrChar.put(str,ch);
CharStr.put(ch,str);
}
return m<0&&sIndex>=n;
}
}
205.同构字符串
难度简单405
给定两个字符串 s 和 *t*,判断它们是否是同构的。
如果 s 中的字符可以按某种映射关系替换得到 *t* ,那么这两个字符串是同构的。
每个出现的字符都应当映射到另一个字符,同时不改变字符的顺序。不同字符不能映射到同一个字符上,相同字符只能映射到同一个字符上,字符可以映射到自己本身。
示例 1:
输入:s = "egg", t = "add" 输出:true示例 2:
输入:s = "foo", t = "bar" 输出:false示例 3:
输入:s = "paper", t = "title" 输出:true提示:
- 可以假设 s 和 *t* 长度相同。
class Solution {
public boolean isIsomorphic(String s, String t) {
HashMap<Character,Character> chst = new HashMap<>();
HashMap<Character,Character> chts = new HashMap<>();
int m=s.length(),n=t.length(),nIndex=0,mIndex=0;
char chs,cht;
if(m!=n) return false;
for(int i=0;i<m;i++){
chs = s.charAt(i);
cht = t.charAt(i);
if(chst.containsKey(chs)&&chst.get(chs)!=cht){
return false;
}
if(chts.containsKey(cht)&&chts.get(cht)!=chs){
return false;
}
chst.put(chs,cht);
chts.put(cht,chs);
}
return true;
}
}
633.平方数的和 (非哈希表)
给定一个非负整数
c,你要判断是否存在两个整数a和b,使得a2 + b2 = c。示例 1:
输入:c = 5 输出:true 解释:1 * 1 + 2 * 2 = 5示例 2:
输入:c = 3 输出:false示例 3:
输入:c = 4 输出:true示例 4:
输入:c = 2 输出:true示例 5:
输入:c = 1 输出:true提示:
0 <= c <= 231 - 1
方法一: 使用 sqrt 函数
解题思路:
- 通过开平方寻找最大平方数n
- 把最大平方数n作为条件递减循环
- 直到找到另外一个被减数为平方数则返回true
- 若一直未找到该数直到循环结束则返回false
class Solution {
public boolean judgeSquareSum(int c) {
int n = (int)Math.sqrt(c)+1; // +1 只是用来抵消 n--
while(n-- > 0)
if(Math.sqrt(c-n*n) == (int)Math.sqrt(c-n*n))
return true;
return false;
}
}

方法二:双指针
- 如果 a^2 + b^2 =c,我们找到了题目要求的一个解,返回 true
- 如果 a^2 + b^2 <c,此时需要将 a 的值加 1,继续查找
- 如果 a^2 + b^2 >c,此时需要将 b 的值减 1,继续查找
你有没有想过存在漏掉得可能性
| 0 | 1 | 2 | 3 | 4 | |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 4 | 9 | 16 |
| 1 | 1 | 2 | 5 | 10 | 17 |
| 2 | 4 | 5 | 8 | 13 | 20 |
| 3 | 9 | 10 | 13 | 18 | 25 |
| 4 | 16 | 17 | 20 | 25 | 32 |

class Solution {
public boolean judgeSquareSum(int c) {
long left = 0;
long right = (long) Math.sqrt(c);
while (left <= right) {
long sum = left * left + right * right;
if (sum == c) {
return true;
} else if (sum > c) {
right--;
} else {
left++;
}
}
return false;
}
}
边栏推荐
- ORA-27300,ORA-27301,ORA-27302,ORA-27303,TNS-2518,TNS-12549,TNS-12560,TNS-00519等告警处理
- What is the value of digital factory management system
- 报表设计丨如何让你的PowerBI看板出彩?
- Some operations about Anaconda (installing software and quickly opening)
- Custom recycleview delete & move animation
- MobileVIT学习笔记
- CBAM学习笔记
- A new technical director asked me to do an IP territorial function~
- Acwing3715. 最少交换次数(冒泡排序法的模拟思路)
- Using pseudo element before to realize equal scaling of elements
猜你喜欢

Guava Cache 原理分析与最佳实践

Automatic classification of e-commerce UGC pictures using Baidu PaddlePaddle easydl

Principle analysis and best practice of guava cache

"Geography language" large model Wenxin Ernie geol and its application

“地理-语言”大模型文心ERNIE-GeoL及应用

The application of building cloud rendering is expanding, and more and more industries are in urgent need of visualization services

LabVIEW learning note 5: you cannot return to the original state after pressing the button

新来个技术总监要我做一个 IP 属地功能~

C语言-入门-语法-指针(十二)

Can single mode and multi-mode of industrial switches replace each other?
随机推荐
Comprehensively design an oppe homepage -- Design of selected accessories on the page
Rust变量特点
What are the practical advantages of digital factory system
Guava Cache 原理分析与最佳实践
Worthington phospholipase A2 study phosphatidylcholine 2-acetylhydrolase
ACM MM 2022 | 浙大提出:点云分割主动学习新SOTA
ADB ~ 隐藏或禁用状态栏和虚拟按键
【2022牛客多校第二场】K-Link with Bracket Sequence I
自研5G芯片商用推迟?未来4年苹果iPhone都将采用高通5G芯片
Dedecms dream weaving last article next article free controllable output link, title, thumbnail, time
Sre related question answering
MySQL data recovery process is based on binlog redolog undo
Custom recycleview delete & move animation
R language uses LROC function of epidisplay package to visualize ROC curve of logistic regression model and output diagnostic table, visualize multiple ROC curves, and use legend function to add legen
屏蔽自动更新描述文件(屏蔽描述文件)
使用百度飞桨EasyDL实现电商UGC图片自动分类
Command line PDF Converter::: fcoder 2PDF
app测试定位方式
What is the value of digital factory management system
Mysql 回表、SQL优化、四种隔离级别、三大日志binlog、redo log、undo log