训练营0926|哈希表系列

 

目录

 242.有效的字母异位词

 349.两个数组的交集

202.快乐数

1.两数之和


 242.有效的字母异位词

只能说论一个==与equals的区别对人的折磨有多大。

面经:==是位运算符,equals是方法,==只是单纯判断值是否相同,引用地址的指向是否同,而可以通过重写equals去判断对象内部

class Solution {
    /**
     * 判断两个字符串是否互为字母异位词--->两个字符串中出现的字母的类型和个数一样
     *
     * @param s
     * @param t
     * @return
     */
    public boolean isAnagram(String s, String t) {
        //hashmap里面放s字符串里面单个字符出现的次数
        HashMap<Character, Integer> hashMapS = new HashMap<>();
        HashMap<Character, Integer> hashMapT = new HashMap<>();
        putStringInHashMap(hashMapS, s);
        putStringInHashMap(hashMapT, t);
        boolean flag = false;
        if (hashMapS.size() == hashMapT.size()) {
            //判断s里面的字符(key)t中是否存在并且他们两个的个数value需要同---->遍历一个hashmap去找另外一个
            for (Map.Entry<Character, Integer> entry : hashMapS.entrySet()) {
                if (hashMapT.containsKey(entry.getKey())) {
                    flag = hashMapT.get(entry.getKey()).equals(entry.getValue())  ? true : false;
                    if (flag == false) {
                        return false;
                    }
                } else {
                    return false;
                }
            }  
            return flag;
        }
        return false;//长度不等的话有不知名的多出来的字符另一个没有
    }

    /**
     * 把字符串里面的<字符,个数>放进hashmap里面
     *
     * @param hashMap
     * @param s
     */
    public void putStringInHashMap(HashMap<Character, Integer> hashMap, String s) {
        for (int i = 0; i < s.length(); i++) {
            if (!hashMap.containsKey(s.charAt(i))) {
                hashMap.put(s.charAt(i), 1);//没出现过计数器置为1
            } else {
                int sum = hashMap.get(s.charAt(i)) + 1;
                hashMap.put(s.charAt(i), sum);//在之前的基础上面加1
            }
        }
    }
}

 349.两个数组的交集

把两个数组里面的数以及他们出现的个数放进hashmap里面,然后去找相交的部分

即A中的key是否在B中存在?存在的话就往结果数组里面添加。

class Solution {
    /**
     * 返回nums1和nums2之间的交集
     *
     * @param nums1
     * @param nums2
     * @return
     */
    public int[] intersection(int[] nums1, int[] nums2) {
        //<num里面的字母,字母对应的下标>
        HashMap<Integer, Integer> hashMapNums1 = new HashMap<>();
        HashMap<Integer, Integer> hashMapNums2 = new HashMap<>();
        putInHash(hashMapNums1, nums1);
        putInHash(hashMapNums2, nums2);
        //返回交集数组
        ArrayList<Integer> ans = new ArrayList<>();
        for (Map.Entry<Integer, Integer> entry : hashMapNums1.entrySet()) {
            if (hashMapNums2.containsKey(entry.getKey())) {
                ans.add(entry.getKey());
            }
        }
        return ans.stream().mapToInt(Integer::intValue).toArray();
        //流的形式将Arraylist转成int数组
    }

    public void putInHash(HashMap<Integer, Integer> hashMap, int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            if (!hashMap.containsKey(nums[i])) {
                hashMap.put(nums[i], 1);//第一次出现
            } else {
                int sum = hashMap.get(nums[i]) + 1;
                hashMap.put(nums[i], sum);
            }
        }
    }
}

202.快乐数

主要是题干里面的无限循环。无限循环代表有重复的数,当有第一个重复的数的时候就退出循环。

class Solution {
    /**
     * 输入:n = 19
     * 输出:true
     * 解释:
     * 1^2 + 9^2 = 82
     * 8^2 + 2^2 = 68
     * 6^2 + 8^2 = 100
     * 1^2 + 0^2 + 0^2 = 1
     * 判断是否无限循环就是这个数如果出现过就直接break,但是没有出现过就能找到1
     *
     * @param n
     * @return
     */
    public boolean isHappy(int n) {
        HashSet<Integer> set = new HashSet<>();
        while (n != 1 && !set.contains(n)) {
            set.add(n);
            n = calculateNum(n);
        }
        return n == 1;
    }

    /**
     * 1^2 + 9^2 = 82
     * 8^2 + 2^2 = 68
     * 6^2 + 8^2 = 100
     * 1^2 + 0^2 + 0^2 = 1
     *
     * @param n
     * @return
     */
    public int calculateNum(int n) {
        int sum = 0;
        while (n > 0) {
            sum += (n % 10) * (n % 10);
            n = n / 10;
        }
        return sum;
    }
}

1.两数之和

 重点是重复的key怎么将其合理的放进hashmap里面并且后面的值不能把前面的覆盖

1.两个重复的值和为target------>在put之前进行判断

2.两个重复的值和不为target-------->取谁都一样

class Solution {
    /**
     * 两个数的和,输出这两个数的下标
     *
     * @param nums
     * @param target
     * @return
     */
    public int[] twoSum(int[] nums, int target) {
        ArrayList<Integer> ans = new ArrayList<>();
        //<值,索引>
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (hashMap.containsKey(target - nums[i]) && (hashMap.get(target - nums[i]) != i)) {//往前看
                ans.add(hashMap.get(target - nums[i]));
                ans.add(i);
                break;
            }
            hashMap.put(nums[i], i);//<值,索引>---避免重复的key覆盖
        }
        return ans.stream().mapToInt(Integer::intValue).toArray();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值