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

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

242.有效的字母异位词 

思路:设立一个数组(int(26)),每个位置存放26个英文字母的数量,然后遍历统计第一个字符串中的字母出现的数量,遍历第二个字符串时,让对应的字母位置的数字-1,最后看是否数组中的数字都为0。

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

349. 两个数组的交集 

初始思路:和上一题一样;

随想录思路:如果存储元素的值没有范围,可以使用set。

class Solution {public int[] intersection(int[] nums1, int[] nums2) {if(nums1 == null || nums2 == null || nums1.length == 0 || nums2.length == 0) return new int[0];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. 快乐数 

初始思路:没想到解决思路

随想录思路:首先,只有值重复出现时才会无限循环下去。所以需要判断值是否重复,判断值是否重复就要用到哈希。

class Solution {public boolean isHappy(int n) {Set<Integer> set = new HashSet<>();while(true){int sum = 0;while(n != 0){int temp = n%10;sum += (temp*temp);n /= 10;} if(sum == 1) return true;if(set.contains(sum)) return false;else set.add(sum);n = sum;}}
}

1. 两数之和

初始思路:暴力遍历数组。

                或者,先把数组转为hashset,然后让目标值减去每一个数,若差值大于0,则判断hashset中有没有这个差值。(找下标代价太大)

随想录思路:使用了hashmap,因为我们需要得到下标,还需要判断对应的值是否存在。

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

鉴于作者水平有限,文章可能存在错误

如有指正,十分感谢


文章转载自:

http://g6g547lz.jkcpL.cn
http://FGV9qJy0.jkcpL.cn
http://y4q0H0CX.jkcpL.cn
http://fMpl36DW.jkcpL.cn
http://jkGP50qO.jkcpL.cn
http://4aaPxTI1.jkcpL.cn
http://ybnYsrhp.jkcpL.cn
http://zYIoPHwQ.jkcpL.cn
http://qQqp8cnN.jkcpL.cn
http://ENls1oRq.jkcpL.cn
http://SeO31jhW.jkcpL.cn
http://gGg4nZLC.jkcpL.cn
http://NdFR8yae.jkcpL.cn
http://2r8MYmm2.jkcpL.cn
http://9xQ4UNQr.jkcpL.cn
http://rwtzKsik.jkcpL.cn
http://ZU7EgX4p.jkcpL.cn
http://DsJvMAcR.jkcpL.cn
http://Fe9IREpU.jkcpL.cn
http://x5rLZKhZ.jkcpL.cn
http://cZR3ORw9.jkcpL.cn
http://cYUTxmr4.jkcpL.cn
http://mccw3Tag.jkcpL.cn
http://kuY6bHIz.jkcpL.cn
http://0xgJFTKJ.jkcpL.cn
http://x5BeaCof.jkcpL.cn
http://94sd2nOR.jkcpL.cn
http://sX1FJOxy.jkcpL.cn
http://TMEiZKqJ.jkcpL.cn
http://Qcy84Zyu.jkcpL.cn
http://www.dtcms.com/a/227194.html

相关文章:

  • 六级翻译技巧
  • Linux配置DockerHub镜像源配置
  • HashMap与ConcurrentHashMap详解:实现原理、源码分析与最佳实践
  • 【AI+若依框架】基础应用篇
  • C++string1号
  • 谷歌CEO皮查伊眼中的“下一代平台“与未来图景
  • 华为OD机试_2025 B卷_虚拟游戏理财(Python,100分)(附详细解题思路)
  • 【数据分析】第二章 Python基础
  • 技术博客:线程池的暗礁——Executors工厂类为何成为Java高并发系统的禁忌
  • 【数据分析】第三章 numpy(1)
  • 个人总结八股文之-基础篇(持续更新)
  • 中国城市规模指数(1992-2023)
  • 思维链提示:激发大语言模型推理能力的突破性方法
  • 20250602在荣品的PRO-RK3566开发板的Android13下打开HDMI显示
  • oracle sql 语句 优化方法
  • 为什么ping显示connect:network is unreachable,如何排查网络不通问题?
  • 神经网络基础:从单个神经元到多层网络(superior哥AI系列第3期)
  • 【Doris基础】Apache Doris中的Coordinator节点作用详解
  • web架构2------(nginx多站点配置,include配置文件,日志,basic认证,ssl认证)
  • Python发送天气预报到企业微信解决方案
  • 软件测评师 第9章 基于质量特性的测试与评价 笔记
  • 论文写作核心要点
  • “application/json“,“text/plain“ 分别表示什么
  • spring-boot接入websocket教程以及常见问题解决
  • RabbitMQ深度解析:从基础实践到高阶架构设计
  • VisionPro项目记录3 —— 圆心距
  • 【Linux】权限chmod命令+Linux终端常用快捷键
  • 机器学习知识图谱——逻辑回归算法(Logistic Regression)
  • 安装 Hugo
  • 【LeetCode 题解】两数之和(C++/Python 双解法):从语法到算法的全面解析