代码随想录算法训练营第六天| 242.有效的字母异位词、 349. 两个数组的交集、202. 快乐数、 1. 两数之和

242.有效的字母异位词

题目链接:https://leetcode.cn/problems/valid-anagram/submissions/531775354/
文章链接:题目链接/文章讲解/视频讲解: https://programmercarl.com/0242.%E6%9C%89%E6%95%88%E7%9A%84%E5%AD%97%E6%AF%8D%E5%BC%82%E4%BD%8D%E8%AF%8D.html

思路

将s中的每个字母都放到哈希表中,key为字母,value为字母出现的个数,然后遍历t,每遇见一个字母,让对应hash表中的字母减一,当hash表中的字母对应的value都为0的时候说明是字母异位词。时间复杂度o(n),空间复杂度o(n)。

public boolean isAnagram(String s, String t) {
    HashMap<Character, Integer> hash = new HashMap<>();
    for (int i = 0; i < s.length(); i++) {
        hash.put(s.charAt(i), hash.getOrDefault(s.charAt(i), 0) + 1);
    }
    for (int i = 0; i < t.length(); i++) {
        char c = t.charAt(i);
        if (hash.containsKey(c)) {
            hash.put(c, hash.get(c) - 1);
        }else {
            return false;
        }
    }
    for (Integer value : hash.values()) {
        if (value != 0)
            return false;
    }
    return true;
}

349. 两个数组的交集

题目链接:https://leetcode.cn/problems/intersection-of-two-arrays/
文章链接:题目链接/文章讲解/视频讲解: https://programmercarl.com/0349.%E4%B8%A4%E4%B8%AA%E6%95%B0%E7%BB%84%E7%9A%84%E4%BA%A4%E9%9B%86.html

思路

遍历nums1,将里面所有的元素映射到hash表中,在遍历nums2,在hash表中查找,若存在放到set集合中去重。时间复杂度o(n),空间复杂度o(n)。

    public int[] intersection(int[] nums1, int[] nums2) {
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        Set<Integer> set = new HashSet<>();
        for (int num1 : nums1) {
            if (!hashMap.containsKey(num1))
                hashMap.put(num1, 1);
        }
        for (int num2 : nums2) {
            if (hashMap.containsKey(num2))
                set.add(num2);
        }
        return set.stream().mapToInt(Integer::intValue).toArray();
    }

202. 快乐数

题目链接:https://leetcode.cn/problems/happy-number/description/
文章链接:题目链接/文章讲解/视频讲解: https://programmercarl.com/0202.%E5%BF%AB%E4%B9%90%E6%95%B0.html

思路

题目中说了会无限循环,那么也就是说求和的过程中,sum会重复出现。 所以这道题目使用哈希法,来判断这个sum是否重复出现,如果重复了就是return false, 否则一直找到sum为1为止。时间复杂度o(n),空间复杂度o(n)。

    public boolean isHappy(int n) {
        HashSet<Integer> set = new HashSet<>();
        while (n != 1 && !set.contains(n)) {
            set.add(n);
            n = getNextNumber(n);
        }
        return n == 1;
    }
    public int getNextNumber(int n) {
        int res = 0;
        while (n > 0) {
            int temp = n % 10;
            res += temp * temp;
            n /= 10;
        }
        return res;
    }

1. 两数之和

题目链接:https://leetcode.cn/problems/two-sum/description/
文章链接:题目链接/文章讲解/视频讲解: https://programmercarl.com/0001.%E4%B8%A4%E6%95%B0%E4%B9%8B%E5%92%8C.html

思路

遍历数组nums,将数组中的元素映射到map中,key为nums[i],value为i,一边遍历一边在map中查找,如果target-nums[i]的值在map中存在,则输出,否则将加入map中完成映射时间复杂度o(n),空间复杂度o(n)。

    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (hashMap.containsKey(target - nums[i])) {
                return new int[]{i, hashMap.get(target - nums[i])};
            } else
                hashMap.put(nums[i], i);
        }
        return new int[]{0};
    }
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值