当前位置: 首页 > news >正文

【算法刷题】leetcode hot 100 哈希篇

文章目录

  • 1. 两数之和
  • 49. 字母异位词分组
  • 128. 最长连续序列
  • 总结


1. 两数之和

在这里插入图片描述

  1. leetcode:https://leetcode.cn/problems/two-sum/description/?envType=study-plan-v2&envId=top-100-liked
  2. 暴力解决:
    public int[] twoSum(int[] nums, int target) {for (int i = 0; i < nums.length; i++) {for (int j = 0; j < nums.length; j++) {if (j == i) {continue;}if (nums[j] == target - nums[i]) {return new int[]{i, j};}}}return new int[]{};}
  1. 哈希解决:
   public int[] twoSum(int[] nums, int target) {// <Value, Index>Map<Integer, Integer> map = new HashMap<>();for (int i = 0;i < nums.length; i++) {int complete = target - nums[i];if (map.containsKey(complete)) {return new int[]{map.get(complete), i};}map.put(nums[i], i);}return new int[]{};}

49. 字母异位词分组

在这里插入图片描述

  1. leetcode:https://leetcode.cn/problems/group-anagrams/description/?envType=study-plan-v2&envId=top-100-liked
  2. 使用排序法:

思路:
(1). 对每个字符串进行排序。
(2). 使用排序后的字符串作为哈希表的键,将相同的字母异位词放在同一组中。

    public List<List<String>> groupAnagrams(String[] strs) {Map<String, List<String>> map = new HashMap<>();for (String str : strs) {char[] chars = str.toCharArray();Arrays.sort(chars);String key = new String(chars);if (!map.containsKey(key)){map.put(key, new ArrayList<>());}map.get(key).add(str);}return new ArrayList<>(map.values());}
  1. 使用字符计数法

思路:
(1). 对于每个字符串,统计其字符出现的频次,使用一个数组或哈希表表示这个频次。
(2). 将频次数组或频次哈希表作为哈希表的键,将字母异位词加入同一组。

    public List<List<String>> groupAnagrams(String[] strs) {Map<String, List<String>> map = new HashMap<>();for (String str : strs) {int[] counts = new int[26];for (char c : str.toCharArray()) {counts[c - 'a']++;}String key = Arrays.toString(counts);if (!map.containsKey(key)) {map.put(key, new ArrayList<>());}map.get(key).add(str);}return new ArrayList<>(map.values());}

128. 最长连续序列

在这里插入图片描述

  1. leetcode:https://leetcode.cn/problems/longest-consecutive-sequence/description/?envType=study-plan-v2&envId=top-100-liked

  2. 解法:
    通过判断一个数字是否是序列的起点来高效检查最长连续序列。

    public int longestConsecutive(int[] nums) {if (nums == null || nums.length == 0) {return 0;}// 将所有数字加入哈希集合HashSet<Integer> set = new HashSet<>();for (int num : nums) {set.add(num);}int max = 0;// 遍历集合for (int num : set) {// 仅从序列的起点开始扩展if (!set.contains(num - 1)) {int currentNum = num;int currentStreak = 1;// 向后扩展序列while (set.contains(currentNum + 1)) {currentNum++;currentStreak++;}// 更新最大长度max = Math.max(max, currentStreak);}}return max;}

总结

在算法题中,使用哈希表(HashMap)是一种非常高效的工具,特别适用于解决需要快速查找、配对、或分组的问题。

  1. 快速查找:
  • 需要在数组或集合中快速查找某个元素是否存在。
  • 示例:两数之和
  1. 分组:
  • 根据某种规则,将元素分为若干组,键是规则标识,值是元素列表。
  • 示例:字母异位词分组
  1. 记录频率或状态:
  • 统计元素出现的频率,或跟踪元素是否被访问。
  1. 配对与索引存储:
  • 存储某个值对应的另一部分信息,比如索引或其余条件。

解题步骤:

  • 明确题意:
    • 确定是否需要频繁查找或分组操作。
  • 确定哈希表结构:
    • Key:作为分组或查找的标识(例如目标值的差值、排序后的字符串)。
    • Value:存储关联的值(例如索引、元素列表等)。
  • 遍历与处理:
    • 遍历数组,根据逻辑将元素存入或从哈希表中取出。
  • 返回结果:
    • 按题目要求返回处理后的结果。

http://www.lryc.cn/news/516653.html

相关文章:

  • linux系统(ubuntu,uos等)连接鸿蒙next(mate60)设备
  • 支付宝实名认证
  • GO随想:GO的并发等待
  • kubernetes第五天
  • 扩散模型论文概述(三):Stability AI系列工作【学习笔记】
  • JVM调优,参数在哪里设置的?
  • 2024年最新Stable Diffusion 新手入门教程,安装使用及模型下载
  • Ubuntu 20.04安装gcc
  • IT运维的365天--024 闲置路由器关闭了dhcp,如何知道它的IP是啥
  • kaggle竞赛:纽约出租车行程时间NYC Taxi Trip Duration
  • Freemarker模板进行判空
  • C++ const关键字(八股总结)
  • Linux 清楚历史命令
  • 服务器双网卡NCCL通过交换机通信
  • Redis哨兵(sentinel)
  • 小白学Pytorch
  • ros2笔记-2.5.3 多线程与回调函数
  • 第5章:Go语言错误处理和异常
  • 题库刷题知识点总结
  • GraphRAG:LLM之Graphrag接入milvus
  • adb使用及常用命令
  • omnipeek分析beacon帧
  • Java数组问题
  • salesforce 可以为同一个简档的同一个 recordtype 的对象设置多种页面布局吗
  • 使用vue项目中,使用webpack模板和直接用vue.config来配置相关插件 区别是什么,具体有哪些提现呢
  • 五、包图
  • 关于重构一点简单想法
  • kafka使用以及基于zookeeper集群搭建集群环境
  • GAN对抗生成网络(二)——算法及Python实现
  • 并发线程(21)——线程池