哈希表的查找与插入及删除

大家好呀,我是小笙,这周我来分享一些哈希表的算法习题

哈希表的查找与插入及删除

217. 存在重复元素

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. 两个数组的交集

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.快乐数

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.单词规律

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.同构字符串

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.平方数的和 (非哈希表)

633. 平方数之和

给定一个非负整数 c ,你要判断是否存在两个整数 ab,使得 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 函数

解题思路:

  1. 通过开平方寻找最大平方数n
  2. 把最大平方数n作为条件递减循环
  3. 直到找到另外一个被减数为平方数则返回true
  4. 若一直未找到该数直到循环结束则返回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;
    }
}

image-20211208210211536

方法二:双指针
  • 如果 a^2 + b^2 =c,我们找到了题目要求的一个解,返回 true
  • 如果 a^2 + b^2 <c,此时需要将 a 的值加 1,继续查找
  • 如果 a^2 + b^2 >c,此时需要将 b 的值减 1,继续查找

你有没有想过存在漏掉得可能性

01234
0014916
11251017
24581320
3910131825
41617202532

image-20211208211235207

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;
    }
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Al_tair

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值