**代码随想录算法训练营第六天 | 哈希表理论基础 | 242.有效的字母异位词| 349. 两个数组的交集 |202. 快乐数 |1. 两数之和 **
代码
/*** 242. 有效的字母异位词 字典解法* 时间复杂度O(m+n) 空间复杂度O(1)*/
class Solution {public boolean isAnagram(String s, String t) {int[] record = new int[26];for (int i = 0; i < s.length(); i++) {record[s.charAt(i) - 'a']++; // 并不需要记住字符a的ASCII,只要求出一个相对数值就可以了}for (int i = 0; i < t.length(); i++) {record[t.charAt(i) - 'a']--;}for (int count: record) {if (count != 0) { // record数组如果有的元素不为零0,说明字符串s和t 一定是谁多了字符或者谁少了字符。return false;}}return true; // record数组所有元素都为零0,说明字符串s和t是字母异位词}
}
题图
心得
帖子
java中的charAt(i)函数_天才小熊猫啊的博客-CSDN博客_charat(1)
java 中的 for(String a:b)_摸鱼飞弹的博客-CSDN博客_for(a:b)
[java用for循环输出数组,Java打印输出数组的三种方式:传统的for循环方式,for each循环,利用Arrays类中的toString方法…_编剧学徒的博客-CSDN博客](=&request_id=&biz_id=102&utm_term=java for(inta:b)&utm_medium=distribute.pc_-task-blog-2allsobaiduweb~default-1-115735240.142v68control,201v4add_ask,213v2t3_esquery_v1&spm=1018.2226.3001.4187)
Java中length、length()、size()区别_Techyu的博客-CSDN博客_nums.length()
这里的return 应该在大的方法(public)里面
record[s.charAt(i) - 'a']++; // 并不需要记住字符a的ASCII,只要求出一个相对数值就可以了//record[s.charAt(i)]++;
代码
class Solution {public int[] intersection(int[] nums1, int[] nums2) {if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {return new int[0];}Set<Integer> set1 = new HashSet<>();Set<Integer> resSet = new HashSet<>();//遍历数组1for (int i : nums1) {set1.add(i);}//遍历数组2的过程中判断哈希表中是否存在该元素for (int i : nums2) {if (ains(i)) {resSet.add(i);}}//将结果几何转为数组return resSet.stream().mapToInt(x -> x).toArray();}
}
心得
请问判断数组为空,为什么nums = null和nums.length() == 0都要写,不是等效的吗?
int[] array = null; 数组为空,此时array不指向任何对象;
int[] array = new int[0]; 定义一个长度为0的数组;
如果写成这样 if(array.length == 0 || array == null)在array == null 为true 的情况下就会报空指针异常(运行时异常)
题图
心得
结果几何转为数组:
return resSet.stream().mapToInt(x -> x).toArray();
nums应该和null挂钩
if (nums1 == null || nums1.length == 0 //if(nums1==0|| nums1.length==0)
代码
class Solution {public boolean isHappy(int n) {Set<Integer> record = new HashSet<>();while (n != 1 && !ains(n)) {//n如果是1,则跳出循环,n如果是重复的,也跳出while(说明这道题是无限循环)record.add(n);n = getNextNumber(n);}return n == 1;//如果是1,返回true,如果是无限循环的数,则返回false}private int getNextNumber(int n) {int res = 0;while (n > 0) {int temp = n % 10;//获取个位数 res += temp * temp;n = n / 10; //获取十位数及其以上的数字}return res;}
}
题图
心得
题目中说了会 无限循环,那么也就是说求和的过程中,sum会重复出现,这对解题很重要!
所以这道题目使用哈希法,来判断这个sum是否重复出现,如果重复了就是return false, 否则一直找到sum为1为止。
有没有返回值也得看力扣的官方有没有给你自定义函数时候有返回值
int temp = n % 10; //获取个位数
n = n / 10; //获取十位数及其以上的数字
return n == 1; 因为返回值上面定义是boolean所以这个是Truefalse
if(TRUE||false) 里面只能是布尔表达式,所以用到==和!=
while(布尔表达式) 也是和if一样
while (n != 1 && !ains(n)) {
//n如果是1,则跳出循环,n如果是重复的,也跳出while(说明这道题是无限循环)
return n == 1;
//如果是1,返回true,如果是无限循环的数,则返回false
java里面的自定义方法在solution中,并列于public,而且要把参数类型都写出来
private int getNextNumbers(int n)
//private int getNextNumbers(n)
自己定义的有返回值,那么就应该写return
为什么这么定义不可以呢?
res += temp*temp; //必须在上面定义int res =0;//不能省略成为int res = temp*temp;
帖子
代码
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]; // 遍历当前元素,并在map中寻找是否有匹配的ainsKey(temp)){res[1] = i;res[0] = (temp);break;}map.put(nums[i], i); // 如果没找到匹配对,就把访问过的元素和下标加入到map中}return res;
}
题图
心得
什么时候使用哈希法,当我们需要查询一个元素是否出现过,或者一个元素是否在集合里的时候,就要第一时间想到哈希法
我们不仅要知道元素有没有遍历过,还有知道这个元素对应的下标,需要使用 key value结构来存放,key来存元素,value来存下标,那么使用map正合适。
使用数组和set来做哈希法的局限。
这道题目中并不需要key有序,选择std::unordered_map 效率更高!(在Java中就对应着HashMap和TreeMap)
这个0和1的顺序也有讲就,如果是升序的数组,那么res[0]应该是先存入map里面的
res[0](temp);
res[1]=i;
if(nums == null || nums.length == 0){return res;} //忘记考虑边界条件,没有健壮性
找到这两个数,有了这个数组,那么就可以直接跳出了,(不然还会一直进行for循环)
本文发布于:2024-02-04 18:36:02,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170713808258426.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |