【代码随想录算法训练营-第六天】【哈希表】242,349,202,1

news/2025/2/13 15:57:38/

242.有效的字母异位词

第一遍

  • 思考
    • 比较简单,用数组就能实现了
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:看到了一篇大佬的文章讲set相关知识的,很厉害。
  • 学习set转数组:set如何转换为数组

第一遍

  • 思考
    • 题目不难,但重点是掌握和了解set相关的知识,包括list、Hashset、treeset
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没有很好的办法;
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了。
    • 下面两张图片我感觉是我理解题目的中重点,大家可以好好品一品在这里插入图片描述在这里插入图片描述
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[]{};}
}

http://www.ppmy.cn/news/1268226.html

相关文章

使用FluentAvalonia组件库快速完成Avalonia前端开发

前言 工欲善其事必先利其器,前面我们花了几篇文章介绍了Avalonia框架以及如何在Avalonia框架下面使用PrismAvalonia完成MVV模式的开发。今天我们将介绍一款重磅级的Avalonia前端组件库,里面封装了我们开发中常用的组件,这样就不用我们自己再写组件了。专注业务功能开发,提…

[BUG记录]UART占用CPUload过高问题

目录 关键词平台说明一、背景二、根本原因三、措施 关键词 嵌入式、C语言、autosar、TDA4 平台说明 项目ValueOSautosar OSautosar厂商vector芯片厂商TI编程语言C&#xff0c;C编译器HighTec (GCC) 一、背景 在基于TDA4开发的域控中使用到了UART打印debug信息&#xff0c;不…

网络编程案例

InetAddress 类 相关方法: getLocalHost&#xff1a;获取本机InetAddress对象。 getByName&#xff1a;根据指定主机名/域名获取ip地址对象。 getHostName&#xff1a;获取InetAddress对象的主机名。 getHostAddress&#xff1a;获取InetAddress对象的地址。 简单使用&am…

20231213给Ubuntu18.04.6LTS新加一块HDD机械硬盘

20231213给Ubuntu18.04.6LTS新加一块HDD机械硬盘 2023/12/13 22:50 rootrootrootroot-X99-Turbo:~$ cat /etc/issue Ubuntu 18.04.6 LTS \n \l sudo fdisk -l rootrootrootroot-X99-Turbo:~$ rootrootrootroot-X99-Turbo:~$ sudo fdisk -lu Disk /dev/sda: 2.7 TiB, 300059298…

Windows系统使用wsl执行shell脚本报错解决

Windows系统使用wsl执行Shell脚本报错解决 Shell脚本的需求说明 判断字符串str1中是否包含字符串str2&#xff0c;使用~操作符 代码编写 #!/bin/bashstr1"hello" str2"llo"if [[ $str1 ~ $str2 ]];thenecho "$str1 contain $str2" fi脚本编写…

2024年网络安全(黑客)——自学

1.网络安全是什么 网络安全可以基于攻击和防御视角来分类&#xff0c;我们经常听到的 “红队”、“渗透测试” 等就是研究攻击技术&#xff0c;而“蓝队”、“安全运营”、“安全运维”则研究防御技术。 2.网络安全市场 一、是市场需求量高&#xff1b; 二、则是发展相对成熟…

Lombok使用方法和总结

Lombok使用方法和总结 大家好&#xff0c;我是免费搭建查券返利机器人赚佣金就用微赚淘客系统3.0的小编&#xff0c;也是冬天不穿秋裤&#xff0c;天冷也要风度的程序猿&#xff01; Lombok使用方法和总结 1. 介绍 在Java开发中&#xff0c;有许多重复且繁琐的代码需要我们…

遇到生产环境.OutOfMemoryError: Metaspace

错误日志Aborting due to java.lang.OutOfMemoryError: Metaspace 2023-12-13 17:31:35.750 [http-nio-8080-exec-26] ERROR [DefaultReportIndustryPolicy.generateReport:2538] - 生成报告出现问题 - timeout executing POST http://srv-cer-file/sys-file/upload - {} feig…