【代码随想录算法训练营-第六天】【哈希表】242,349,202,1

242.有效的字母异位词

第一遍

  • 思考
    • 比较简单,用数组就能实现了
java 复制代码
class Solution {
    public boolean isAnagram(String s, String t) {
        int[] checkListi = new int[256];
        int[] checkListj = new int[256];
        for (int i = 0; i < s.length(); i++) {
            char checkChar = s.charAt(i);
            int i_ascii = checkChar - '0';
            checkListi[i_ascii] += 1;
        }
        for (int j = 0; j < t.length(); j++) {
            char checkChar = t.charAt(j);
            int j_ascii = checkChar - '0';
            checkListj[j_ascii] += 1;
        }
        return Arrays.equals(checkListi, checkListj);
    }
}

349. 两个数组的交集

第一遍

  • 思考
    • 题目不难,但重点是掌握和了解set相关的知识,包括list、Hashset、treeset
java 复制代码
class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        Set<Integer> set1 = new HashSet<>();
        Set<Integer> set2 = new HashSet<>();
        for (int i : nums1) {
            set1.add(i);
        }
        for (int i : nums2) {
            if (set1.contains(i)) {
                set2.add(i);
            }
        }

        return set2.stream().mapToInt(Integer::intValue).toArray();
    }
}

202. 快乐数

第一遍

  • 思考
    • 题目中说了会 无限循环,那么也就是说求和的过程中,sum会重复出现,这对解题很重要!(这一步如果能想出来,才是真的理解题目的,但是我好像是误打误撞了,只是惯性思维觉得应该用set判断是否有重复出现,没有真正读懂题意)
    • 题目的重点还是看到是否有出现过,要学会使用set
    • 难点:如何取出每一位的数值;这一步想了一下,没有过多思考,答案应该有很简单的方法,就直接看了,自己想了5mins没有很好的办法;
java 复制代码
class Solution {
    public boolean isHappy(int n) {
            Set<Integer> set1 = new HashSet<>();
            boolean flag = true;
            while (flag) {
                n = sum(n);
                if (n == 1) {
                    return flag;
                }
                flag = set1.add(n);
            }

            return flag;
        }

        public int sum(int n) {
            int result = 0;
            while (n > 0) {
                int mod = n % 10;
                result += mod * mod;
                n /= 10;
            }
            return result;
        }
}

1. 两数之和

第一遍

  • 思考
    • 根据建议,先看了题解,了解到了本题应该使用的思路,了解了一下java相关的map的用法,一遍AC了。
    • 下面两张图片我感觉是我理解题目的中重点,大家可以好好品一品
java 复制代码
package HashStructure;

import java.util.*;

public class HashTest {
    public static void main(String[] args) {
        int[] nums1 = {2,7,11,15};
        int target = 9;
        Solution solution = new Solution();
        int[] result = solution.twoSum(nums1, target);
        System.out.println(Arrays.toString(result));
    }

}

class Solution {
    public int[] twoSum(int[] nums, int target) {
        Map<Integer, Integer> map1 = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int findValue = target - nums[i];
            if (map1.containsKey(findValue)) {
                return new int[]{i, map1.get(findValue)};
            } else {
                map1.put(nums[i], i);
            }
        }
        return new int[]{};
    }
}
相关推荐
zhangfeng113332 分钟前
国产GPU/AI算力芯片现状(修订完整版 · 截至2026年9月
人工智能·算法·ai编程·npu
学代码的CJY1 小时前
从代码出发理解时间复杂度与空间复杂度
数据结构·算法
白帽攻防录1 小时前
SRC 挖洞:WSO2 JWT 算法混淆绕过深度复盘,CVE-2026-5430 不支持的算法怎么变成管理员
网络·算法·安全·网络安全·jwt
小O的算法实验室2 小时前
IEEE TEVC,基于精确与DQN群智能调度方法用于多目标异构USV
算法
字节跳动的猫2 小时前
LikeShop 积分体系全链路改造:获取规则、订单抵扣与积分商品兑换二开
运维·数据结构
可乐ea2 小时前
AI Agent 工具调用准确性评测:选择错误与参数错误分开测
大数据·人工智能·算法·大模型·工具调用·ai智能体·agent评测
上位机妹子3 小时前
C 语言 数组删除指定元素(快慢指针法)
c语言·数据结构·算法
心中有你02143 小时前
【路径规划】A*寻路算法最通俗易懂讲解(C语言完整实现+详细注释)
c语言·开发语言·算法
papaofdoudou4 小时前
初中数学-整数整除判定法全解:从 2 到 19 的方法、依据与证明
算法
让学习成为一种生活方式4 小时前
啤酒花基因组与重测序--Nature Communications
人工智能·算法