【代码随想录算法训练营-第六天】【哈希表】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[]{};
    }
}
相关推荐
抠脚学代码10 分钟前
Linux开发-->驱动开发-->字符设备驱动框架
linux·数据结构·驱动开发
报错小能手20 分钟前
C++笔记(面向对象)静态联编和动态联编
开发语言·c++·算法
WBluuue26 分钟前
AtCoder Beginner Contest 430(ABCDEF)
c++·算法
小肖爱笑不爱笑29 分钟前
2025/11/5 IO流(字节流、字符流、字节缓冲流、字符缓冲流) 计算机存储规则(ASCII、GBK、Unicode)
java·开发语言·算法
熬了夜的程序员1 小时前
【LeetCode】99. 恢复二叉搜索树
算法·leetcode·职场和发展
Kent_J_Truman1 小时前
LeetCode Hot100 自用
算法·leetcode·职场和发展
还是码字踏实1 小时前
算法题种类与解题思路全面指南:基于LeetCode Hot 100与牛客Top 101
算法·leetcode
Victory_orsh2 小时前
“自然搞懂”深度学习(基于Pytorch架构)——010203
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
CoovallyAIHub2 小时前
突破360°跟踪极限!OmniTrack++:全景MOT新范式,HOTA指标狂飙43%
深度学习·算法·计算机视觉
得物技术2 小时前
得物管理类目配置线上化:从业务痛点到技术实现
后端·算法·数据分析