哈希表的查找与插入及删除
Posted Al_tair
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了哈希表的查找与插入及删除相关的知识,希望对你有一定的参考价值。
哈希表的查找与插入及删除
大家好呀,我是小笙,这周我来分享一些哈希表的算法习题
哈希表的查找与插入及删除
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;
以上是关于哈希表的查找与插入及删除的主要内容,如果未能解决你的问题,请参考以下文章