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

算法训练营day06 哈希表(统计数,去重,降低时间复杂度)

💡 解题思路

  1. 📝 确定输入与输出
  2. 🔍 分析复杂度
  3. 🔨 复杂题目拆分严谨且完整 地拆分为更小的子问题(哈希表的使用场景)–(多总结
  4. 💭 选择处理逻辑: 根据拆分后的子问题,总结并选择合适的问题处理思路
  5. 🔎 检查特殊情况:边界条件和特殊情况
  6. 🏁 返回结果

● 242.有效的字母异位词 (做统计)

class Solution {public boolean isAnagram(String s, String t) {if (s.length() != t.length()) {return false;}int[] table = new int[26];for (int i = 0; i < s.length(); i++) {table[s.charAt(i) - 'a']++;}for (int i = 0; i < t.length(); i++) {table[t.charAt(i) - 'a']--;if (table[t.charAt(i) - 'a'] < 0) return false;}return true;}
}

● 349. 两个数组的交集 (Set 去重)

class Solution {public static int[] intersection(int[] nums1, int[] nums2){if (nums1.length == 0 || nums2.length == 0) {return new int[0];}int len1 = nums1.length, len2 = nums2.length;Set<Integer> set = new HashSet<>(); // 确保不重复for (int num : nums1) {set.add(num);}List<Integer> res = new ArrayList<>(); // 动态数组记录结果for (int num : nums2) {if (set.contains(num)) {res.add(num);set.remove(num);}}int[] result = new int[res.size()];for (int i = 0; i < res.size(); i++) {result[i] = res.get(i);}return result;}
}

● 202. 快乐数(快慢指针的实际应用(变体)

class Solution {public boolean isHappy(int n) {if (n == 1 || getNext(n) == 1) return true;int slow = n;int fast = getNext(n);while (fast != 1 && slow != fast) {slow = getNext(slow);fast = getNext(getNext(fast));}return fast == 1;}private int getNext(int n) {int totalSum = 0;while (n > 0) {int d = n % 10;n = n / 10;totalSum += d * d;}return totalSum;}
}

● 1. 两数之和 (哈希表降低时间复杂度)

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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Springboot项目接入支付宝SDK
  • py基础语法简述
  • 基于Java的微信记账小程序【附源码】
  • 顺序表的应用
  • unity对于文件夹的操作
  • 大数据面试题之Flink(1)
  • 如何网页在线编辑微软Office Word,并导出为PDF格式。
  • APP渗透-android12夜神模拟器+Burpsuite实现
  • uni-app上传失败超出文件限制解决方法-分包处理-预加载
  • Android --- 新电脑安装Android Studio 使用 Android 内置模拟器电脑直接卡死,鼠标和键盘都操作不了
  • AI绘画Stable Diffusion【图生图教程】:图片高清修复的三种方案详解,你一定能用上!(附资料)
  • MSPM0G3507——解决printf重定向在其他位置不能用的问题(printf重定向的补充)
  • 【密码学】密码学五要素
  • 使用c++进行大规模的矩阵运算
  • Debezium日常分享系列之:Debezium 2.7.0.Final发布
  • [PHP内核探索]PHP中的哈希表
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • ES6, React, Redux, Webpack写的一个爬 GitHub 的网页
  • github指令
  • iOS编译提示和导航提示
  • MySQL Access denied for user 'root'@'localhost' 解决方法
  • 买一台 iPhone X,还是创建一家未来的独角兽?
  • 消息队列系列二(IOT中消息队列的应用)
  • 一些css基础学习笔记
  • 一些关于Rust在2019年的思考
  • 国内开源镜像站点
  • 如何用纯 CSS 创作一个货车 loader
  • 移动端高清、多屏适配方案
  • ​经​纬​恒​润​二​面​​三​七​互​娱​一​面​​元​象​二​面​
  • #QT(智能家居界面-界面切换)
  • (1)(1.13) SiK无线电高级配置(五)
  • (HAL)STM32F103C6T8——软件模拟I2C驱动0.96寸OLED屏幕
  • (JSP)EL——优化登录界面,获取对象,获取数据
  • (二)基于wpr_simulation 的Ros机器人运动控制,gazebo仿真
  • (二十一)devops持续集成开发——使用jenkins的Docker Pipeline插件完成docker项目的pipeline流水线发布
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (免费领源码)python#django#mysql校园校园宿舍管理系统84831-计算机毕业设计项目选题推荐
  • (欧拉)openEuler系统添加网卡文件配置流程、(欧拉)openEuler系统手动配置ipv6地址流程、(欧拉)openEuler系统网络管理说明
  • (十八)用JAVA编写MP3解码器——迷你播放器
  • (淘宝无限适配)手机端rem布局详解(转载非原创)
  • (提供数据集下载)基于大语言模型LangChain与ChatGLM3-6B本地知识库调优:数据集优化、参数调整、Prompt提示词优化实战
  • (一)Spring Cloud 直击微服务作用、架构应用、hystrix降级
  • (转)IOS中获取各种文件的目录路径的方法
  • (转)Sublime Text3配置Lua运行环境
  • (转载)(官方)UE4--图像编程----着色器开发
  • .bat批处理(一):@echo off
  • .NET Core 发展历程和版本迭代
  • .Net 执行Linux下多行shell命令方法
  • .NET6 开发一个检查某些状态持续多长时间的类
  • .net开发时的诡异问题,button的onclick事件无效
  • @AutoConfigurationPackage的使用
  • [ vulhub漏洞复现篇 ] Apache APISIX 默认密钥漏洞 CVE-2020-13945
  • [《百万宝贝》观后]To be or not to be?
  • [16/N]论得趣
  • [240903] Qwen2-VL: 更清晰地看世界 | Elasticsearch 再次拥抱开源!