今日学习的文章链接和视频链接
https://programmercarl.com/%E5%93%88%E5%B8%8C%E8%A1%A8%E7%90%86%E8%AE%BA%E5%9F%BA%E7%A1%80.html
自己看到题目的第一想法
( 242.有效的字母异位词 )先判断长度是否一致,不一致,返回false.一致继续下面逻辑。s和t两个字符串均为26个小写字母组成,则可以通过创建一个26个字符的数组用以记录s和t两字符串中出现相应字符的次数,记录方式为:遍历两字符串分别进行+操作和-操作,最终对数组进行遍历。如果存在不为0的数,则两字符串一定不等,否则,返回true.
( 349. 两个数组的交集 )定义可变数组,两层for循环遍历判等加入新数组。返回即可
(第202题. 快乐数)取每一位的数字进行平方加和,以此循环判断是否为1,为1则输出,否则进入死循环退出。
(1. 两数之和)两层for循环,遇到和等于输出即可。但不通过就很怪,没看出来哪有漏洞??
看完代码随想录之后的想法
(242.有效的字母异位词)逻辑相同。
(349. 两个数组的交集)为什么用set?
(第202题. 快乐数)逻辑很清晰,实现的每一步细节都有些陌生。比如:设定函数和新集合,集合方法调用。
(1. 两数之和)map数据结构的应用,没有漏洞。
自己实现过程中遇到哪些困难
( 242.有效的字母异位词 )判断相等的细节以及加减方式存在问题。
(349. 两个数组的交集)很难实现,数据结构很陌生,元素去重!
(第202题. 快乐数)怎么取每一位数字?怎么引用循环判定?
(1. 两数之和)map的两个数据分别代表什么?
练习结果
242.有效的字母异位词
1// 哈希表法
2class Solution {
3 public boolean isAnagram(String s, String t) {
4 int[] res = new int[26];
5 // 该判断可以舍弃
6 if (s.length() != t.length()) {
7 return false;
8 }
9
10 for (int i = 0; i < s.length(); i++) {
11 res[s.charAt(i) - 'a']++; // 判等的实现方式
12 }
13
14 for (int i = 0; i < t.length(); i++) {
15 res[t.charAt(i) - 'a']--;
16 }
17
18 for (int count : res) {
19 if (count != 0) {
20 return false;
21 }
22 }
23
24 return true;
25
26 }
27}
28
29// 排序法
30class Solution {
31 public boolean isAnagram(String s, String t) {
32 if (s.length() != t.length()) {
33 return false;
34 }
35 char[] str1 = s.toCharArray();
36 char[] str2 = t.toCharArray();
37 Arrays.sort(str1);
38 Arrays.sort(str2);
39 return Arrays.equals(str1, str2);
40 }
41}
(349. 两个数组的交集)
1// 自己实现(感觉逻辑没错,但编译都有问题)
2class Solution {
3 public int[] intersection(int[] nums1, int[] nums2) {
4 int[] res = new int[1000];
5
6
7 for (int i = 0; i < nums1.length; i++) {
8 for (int j = 0; j < nums2.length; j++) {
9 if (nums1[i] == nums2[j]) {
10 res[i] = nums1[i];
11
12 break;
13 }
14 }
15 }
16 return res;
17 }
18}
19
20// 抄了一遍
21import java.util.HashSet;
22import java.util.Set;
23
24
25class Solution {
26 public int[] intersection(int[] nums1, int[] nums2) {
27 // nums1 == null 是判断 nums1 是否为 null,即判断 nums1 是否为一个空引用。如果 nums1 是 null,则表示数组未被初始化或者没有被赋值。
28// nums1.length == 0 是判断 nums1 数组的长度是否为0。如果 nums1 的长度为0,表示数组中没有元素。
29 if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
30 return new int[0];
31 }
32
33 Set<Integer> set1 = new HashSet<>();
34 Set<Integer> resSet = new HashSet<>();
35
36 for (int i : nums1) {
37 set1.add(i);
38 }
39 for (int i : nums2) {
40 if (set1.contains(i)) {
41 resSet.add(i);
42 }
43 }
44
45 //方法1:将结果集合转为数组
46 // return resSet.stream().mapToInt(x -> x).toArray();
47 // resSet.stream():将resSet转换为一个流(Stream)对象。 这里假设resSet是一个Set<Integer>类型的集合。
48 // .mapToInt(x -> x):对流中的每个元素进行映射操作,将其转换为整型。 这里的x -> x表示一个Lambda表达式,将流中的每个元素x映射为自身。
49 // .toArray():将流中的元素转换为一个整型数组。 这里将流中的元素收集到一个数组中。
50 // 综合起来,这段代码的作用是将一个Set<Integer>类型的集合resSet转换为一个整型数组。
51
52 // 方法2:将结果集合转为数组
53 int[] arr = new int[resSet.size()];
54 int j = 0;
55 for (int i : resSet) {
56 arr[j++] = i;
57 }
58 return arr;
59 }
60}
(第202题. 快乐数)
1//抄了一遍
2class Solution {
3 public boolean isHappy(int n) {
4 Set<Integer> record = new HashSet<>();
5 while (n != 1 && !record.contains(n)) {
6 record.add(n);
7 n = getNextNumber(n);
8 }
9 return n == 1;
10 }
11
12 private int getNextNumber(int n) {
13 int res = 0;
14 while (n > 0) {
15 res += (n % 10) * (n % 10);
16 n = n / 10;
17 }
18 return res;
19 }
20}
(1. 两数之和)
1// 自己实现错误逻辑
2class Solution {
3 public int[] twoSum(int[] nums, int target) {
4 int[] res = new int[2];
5 for (int i : nums) {
6 for (int j : nums) {
7 if (nums[i] + nums[j] == target) {
8 res[0] = i;
9 res[1] = j;
10 break;
11 }
12 }
13 }
14
15 return res;
16 }
17}
18
19// 抄了一遍又一遍
20class Solution {
21 public int[] twoSum(int[] nums, int target) {
22 int[] res = new int[2];
23 if (nums == null || nums.length == 0) {
24 return res;
25 }
26 Map<Integer, Integer> map = new HashMap<>();
27 for (int i = 0; i < nums.length; i++) {
28 int temp = target - nums[i];
29 if (map.containsKey(temp)) {
30 res[1] = i;
31 res[0] = map.get(temp);
32 break;
33 }
34 map.put(nums[i], i);
35 }
36 return res;
37 }
38}
今日收获,记录一下自己的学习时长
4h
以上