当前位置:网站首页>哈希表的查找与插入及删除
哈希表的查找与插入及删除
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;
}
}
边栏推荐
- DAY_ 4. Operation -- judge whether there is a certain data in the array -- realize array mapping (enlarge by 10 times) -- insert the array in sequence (modify bugs) -- realize array de duplication
- Worthington磷脂酶A2研究丨磷脂酰胆碱2-乙酰水解酶
- Command line PDF Converter::: fcoder 2PDF
- ORA-27300,ORA-27301,ORA-27302,ORA-27303,TNS-2518,TNS-12549,TNS-12560,TNS-00519等告警处理
- 综合设计一个OPPE主页--页面的精选配件的设计
- In crsctl, the function of displayed home
- MySQL back to table, SQL optimization, four isolation levels, three logs binlog, redo log, undo log
- mysql 最大建议行数2000w,靠谱吗?
- Comprehensively design an oppe home page -- the style of the search and oper part of the page
- Conquer 3 pieces of IT equipment for all programmers →
猜你喜欢

Explain cache consistency and memory barrier

mysql 最大建议行数2000w,靠谱吗?

Good driving, inexpensive, comfortable and safe! Experience BYD song Pro DM-I in depth

Some operations about Anaconda (installing software and quickly opening)

Design of noise reduction link based on DSP

Five celebrities' worries about AI

The use experience of the product is up to you in the evaluation and exchange meeting of the flying oar frame experience!

STL源码剖析
![[2022 Niuke multi School Game 2] k-link with bracket sequence I](/img/95/9d6710bfb7b9282b4a06a5f61a1f08.png)
[2022 Niuke multi School Game 2] k-link with bracket sequence I

新来个技术总监要我做一个 IP 属地功能~
随机推荐
STL源码剖析
University of Tilburg, Federal University of the Netherlands | neural data to text generation based on small datasets: comparing the added value of two semi supervised learning approvals on top of a l
Explain cache consistency and memory barrier
A review of component parsing (Second Edition)
What are the practical advantages of digital factory system
ECCV 2022 | 中科大&京东提出:数据高效的Transformer目标检测器
中英文说明书丨人甲胎蛋白(AFP)ELISA定量试剂盒
Summary of common methods and attributes of arrays and strings in JS
A new technical director asked me to do an IP territorial function~
PostgreSQL source code (65) analysis of the working principle of globalvis, a new snapshot system
R language uses t The test function performs a t-test to verify whether the population mean is a specific value (inferring the population mean from the sample set)
最高7.5Gbps!全球首款5nm 5G基带骁龙X60发布:支持聚合全部主要频段!
"Geography language" large model Wenxin Ernie geol and its application
Acwing3715. Minimum exchange times (simulation idea of bubble sorting method)
Plato Farm在Elephant Swap上铸造的ePLATO是什么?为何具备高溢价?
ADB ~ hide or disable the status bar and virtual keys
ORA-27300,ORA-27301,ORA-27302,ORA-27303,TNS-2518,TNS-12549,TNS-12560,TNS-00519等告警处理
Behavior level description and RTL level description
Pytest失败重跑
mysql 最大建议行数2000w,靠谱吗?