leetcode打卡-哈希表I:242.有效的字母异位词 349. 两个数组的交集 202. 快乐数 1. 两数之和

文章提供了四道LeetCode算法题的解决方案,包括检查两个字符串是否为字母异位词、找出两个整数数组的交集、判断一个数是否为快乐数以及找到数组中使得数字和为目标值的两个数。所有方法均利用了哈希表来优化求解过程。

目录

242.有效的字母异位词

349. 两个数组的交集

 202. 快乐数

 1. 两数之和 


242.有效的字母异位词

leetcode题目链接:https://leetcode.cn/problems/valid-anagram

leetcode AC记录:

思路:

        1. hashmap存储字符对应的出现次数,对于字符串s,出现一次加一次,对于字符串t,出现一次减一次;

        2.遍历hashmap,判断字符出现次数是否都为0,符合返回true,否则false;

代码如下:

public boolean isAnagram(String s, String t) {
        Map<Character, Integer> map = new HashMap<>(s.length());
        for(int i = 0; i < s.length();i++) {
            map.put(s.charAt(i), map.getOrDefault(s.charAt(i), 0) + 1);
        }

        for(int i = 0; i < t.length();i++) {
            map.put(t.charAt(i), map.getOrDefault(t.charAt(i), 0) - 1);
        }

        for(Map.Entry<Character, Integer> entry : map.entrySet()) {
            if(entry.getValue() != 0) {
                return false;
            }
        }

        return true;
        
    }

349. 两个数组的交集

leetcode题目链接:https://leetcode.cn/problems/intersection-of-two-arrays 

leetcode AC记录:

思路:

用hashset记录第一个数组中的数字,遍历第二个数组,如果存在hashset中,添加到结果hashset中;

代码如下:

public int[] intersection(int[] nums1, int[] nums2) {
        HashSet<Integer> set = new HashSet<>();
        for(int i = 0; i < nums1.length;i++) {
            set.add(nums1[i]);
        }

        HashSet<Integer> res = new HashSet<>();
        for(int i = 0;i < nums2.length;i++)  {
            if(set.contains(nums2[i])) {
                res.add(nums2[i]);
            }
        }

        if(res.size() == 0) {
            return new int[0];
        }

        int[] resArr = new int[res.size()];
        int index = 0;
        for(Integer num : res) {
            resArr[index++] = num;
        }
        return resArr;
    }

 202. 快乐数

leetcode题目链接:https://leetcode.cn/problems/happy-number

leetcode AC记录:

思路:

1.快乐数不会一直增大;

2.计算待求数字的位数;

3.按位加和,放到hashset中,如果重复跳出循环;

代码如下:

public boolean isHappy(int n) {
        HashSet<Integer> set = new HashSet<>();


        while(true) {
            int happy = getHappy(n);
            if(happy == 1) {
                return true;
            } else if(set.contains(happy)) {
                return false;
            } else {
                set.add(happy);
            }

            n = happy;
        }
    }

    public static int getWeishu(int n) {
        int count = 0;
        while(n > 0) {
            n /= 10;
            count++;
        }

        return count;
    }


    public int getHappy(int n) {
        int weishu = getWeishu(n);
        int jishu = 1;
        while(weishu-- > 1) {
            jishu *= 10;
        }

        int num = 0;
        int sum = 0;
        while(n > 0) {
            num = n / jishu;
            sum += (num * num);
            n -= (num * jishu);
            jishu /= 10;
        }

        return sum;
    }

 1. 两数之和 

leetcode题目链接:https://leetcode.cn/problems/two-sum

leetcode AC记录:

思路:

1.使用map存放数字和对应下标;

2.遍历数组,先判断map中是否存在当前 target-数字的key,如果存在,存放结果并返回;

3.如果不存在,将当前数字和对应下标存放进map;

代码如下:

public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> differ = new HashMap<>();
        for(int i = 0;i < nums.length;i++) {
            if(differ.containsKey(target - nums[i])){
                int[] res = new int[2];
                res[0] = i;
                res[1] = differ.get(target-nums[i]);
                return res;
            } else {
                differ.put(nums[i], i);
            }
        }
        return new int[0];
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值