代码随想录刷题day06|异位词&两个数组的交集&两数之和&快乐数


day06学习内容

day04主要内容

  • 有效的字母异位词
  • 两个数组的交集
  • 两数之和
  • 快乐数

一、 有效的字母异位词

242原题链接

核心思路

1、定义一个数组叫做result用来记录字符串s里字符出现的次数。
2、需要把字符映射到数组也就是哈希表的索引下标上。
3、遍历字符串s的时候,将 s[i] - ‘a’ 所在的元素做+1 操作,这样就将字符串s中字符出现的次数统计出来了。
4、如何检查字符串t中是否出现了这些字符,
5、遍历字符串t的时候,对t中出现的字符映射哈希表索引上的数值再做-1的操作。
6、最后检查一下,result数组如果有某个元素不为零0,说明字符串s和t一定是谁多了字符或者谁少了字符,return false。

问题,为什么要s[i] - ‘a’

  • 在Java中,String类有一个方法叫做charAt(int index),它用于返回指定索引位置的char值。索引范围是从0到字符串的长度减1。
  • 在下文的isAnagram方法中,s.charAt(i)是用来获取字符串s中第i个位置的字符的。这里的i是循环变量,从0开始,直到s.length() - 1。
  • 例如,假设我们有一个字符串s = “hello”:
    • 当i = 0时,s.charAt(i)会返回’h’。
    • 当i = 1时,s.charAt(i)会返回’e’。
    • 以此类推…
  • 在下文的isAnagram方法中,s.charAt(i)被用来遍历字符串s中的每个字符,并统计每个字符出现的次数。这是通过result[s.charAt(i) - ‘a’]++这一行代码完成的,它将对应字母在result数组中的计数增加1。
    • 这里,s.charAt(i) - 'a’的目的是将字符转换为其在字母表中的位置(即,'a’变为0,'b’变为1,等等)

1.错误写法1

class Solution {
    public boolean isAnagram(String s, String t) {
        int[] result = new int[26];
        for (int i = 0; i < s.length(); i++) {
            result[s.charAt(i) - 'a']++;
        }
        for (int j = 0; j < t.length(); j++) {
            result[t.charAt(j) - 'a']--;
        }
        for (int num : result) {
            if (num != 0) {
                return false;
            }
            return true;//不是,你在这里return true干什么。
        }
        return true;
    }
}

2.正确写法1

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

二、两个数组的交集

249原题链接

思路

计算两个数组的交集。简单的方法是遍历第一个数组,对于其中的每个元素,判断该元素是否在在第二个数组中。如果存在,就说明是交集,就需要把该元素添加到返回值。

比较简单的一题,关键是要有思路吧。

1.正确写法1

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        HashSet<Integer> set1 = new HashSet<>();
        HashSet<Integer> set2 = new HashSet<>();
        //遍历第一个数组,无脑把所有元素都添加进去
        for (int i = 0; i < nums1.length; i++) {
            set1.add(nums1[i]);
        }
        //遍历第二个数组,只添加在2个数组中都存在的元素
        for (int i = 0; i < nums2.length; i++) {
            if (set1.contains(nums2[i])) {
                set2.add(nums2[i]);
            }
        }

        //for循环把共有元素返回
        int[] result = new int[set2.size()];
        int j = 0;
        for (int i : set2) {
            result[j++] = i;
        }
        return result;
    }
}

三、两数之和

1原题链接
一句话,梦开始的地方,多少次从这一题开始刷起没有坚持下来的了。hhhhh

1.错误写法

class Solution {
    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map1 = new HashMap<>();
        int i = 0;
        int[] result = new int[2];
        while (i < nums.length) {
            int temp = target - nums[i];
            //声明了一个temp,不用你是要干嘛。
            if (map1.containsKey(nums[i])) {
                result[0] = map1.get(nums[i]);
                result[1] = i;
                return result;
            }
            map1.put(nums[i], i);
            i++;
        }
        return result;
    }
}

1.正确写法-hashmap

class Solution {
    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map1 = new HashMap<>();
        int i = 0;
        int[] result = new int[2];
        while (i < nums.length) {
            int temp = target - nums[i];
            if (map1.containsKey(temp)) { //注意这里不要写错了
                result[0] = map1.get(temp);
                result[1] = i;
                return result;
            }
            map1.put(nums[i], i);
            i++;
        }
        return result;
    }
}

1.1.扩展-能不能调换顺序问题

顺序换成下面这样行不行,答案是可以的。

result[0] = i;
result[1] = map1.get(temp);

class Solution {
    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> map1 = new HashMap<>();
        int i = 0;
        int[] result = new int[2];
        while (i < nums.length) {
            int temp = target - nums[i];
            if (map1.containsKey(temp)) { //注意这里不要写错了
                result[0] = i;
                result[1] = map1.get(temp);
                return result;
            }
            map1.put(nums[i], i);
            i++;
        }
        return result;
    }
}

为什么可以???
看题目:
可以任意顺序

四、快乐数

202原题链接

1.思考

核心思路

  • 先看懂题目,看疑问1
  • 怎么拆分成个位数求和.看疑问2

疑问1 : 7为什么是快乐数

72+02=49;
42 + 92 = 97;
92 + 72 = 130;
12 + 32+02 = 10;
12 + 02 = 1;
很明显,按照题意,7是快乐数,一开始比较笨,没有想到7可以表示成 72+02=49,注意0^2^,很重要

疑问2 : 为什么n % 10

取余,就是用来取个位数求和的

疑问3 : 为什么要n / 10

判断有没有大于10的数(或者说有没有大于个位数的数),如果有,那么就要继续拆成个位数求和。

2.正确写法

class Solution {
    public boolean isHappy(int n) {
        Set<Integer> result = new HashSet<>();
        while (n != 1 && !result.contains(n)) {
            result.add(n);
            n = getNext(n);
        }
        return n == 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.感想

  • 快乐数竟然是简单难度的题目,不看题解我是写不了一点的。
  • 快乐数抄的题解,暂时写了一下自己的理解,后续还是要多想想。
  • 今天好的时间比较久,一共用了4个小时。

2.思维导图

本文思路引用自代码随想录,感谢代码随想录作者。

  • 12
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值