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

一、 LeetCode242.有效的字母异位词

1. 题目链接LeetCode242.有效的字母异位词
2. 学习资料:
3. 思路
  1. 选择哈希表来解题
  2. 因为题目要求中表明了s 和 t 仅包含小写字母,所以字母只会出现26个情况,比较小,所以我们用数组 record[26]
  3. 小写字母’a’ ~ 'z’对应到数组 record数组的 索引为0 ~ 25的位置上,所以每个字母都与’a’相减,差值即为这个字母在数组中的坐标
  4. 如果最终数组中的各个位置都为0,说明这两个字符串护卫字母异位词,否则不为字母异位词。
4. 代码
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']++;
        }
        for(int i = 0; i< t.length();i++){
            record[t.charAt(i)-'a']--;
        }
        for(int count:record){
            if(count!=0){
                return false;
            }
        }
        return true;
    }
}

二、 LeetCode349. 两个数组的交集

1. 题目链接LeetCode349. 两个数组的交集
2. 学习资料:
3. 思路
  1. 用哈希结构的关键词是题目要求判断有没有某个元素,当我们遇到了要快速判断一个元素是否出现集合里的时候,就要考虑哈希法了。
  2. 将int数组nums1中的元素放入到哈希表中
  3. 遍历nums2中的元素,如果哈希表中有那么久放到第二个哈希数组中。
  4. 将第二个哈希数组中的元素存放到result数组中,最后返回result数组。
4. 代码
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> set2 = new HashSet<>();
        for(int i: nums1){
            set1.add(i);
        }
        for(int i: nums2){
            if(set1.contains(i)){
                set2.add(i);
            }
        }
        int[] result = new int[set2.size()];
        int index = 0;
        for(int i : set2){
            result[index++] = i;
        }
        return result;

    }
}

三、 LeetCode202. 快乐数

1. 题目链接LeetCode202. 快乐数
2. 学习资料:
3. 思路
  1. 题目中说了会 无限循环,那么也就是说求和的过程中,sum会重复出现,这对解题很重要!
  2. 如果sum重复了就是return false, 否则一直找到sum为1为止。
4. 代码
class Solution {
    public boolean isHappy(int n) {
        Set<Integer> record = new HashSet<>();
        while(n!=1 && !record.contains(n)){
            record.add(n);
            n = getNextNum(n);
        }

        return n == 1;

    }
    public int getNextNum(int n){
        int res = 0;
        while(n>0){
            int t = n%10;
            res = res+ t*t;
            n = n/10;
        }
        return res;
    }
}

四、 LeetCode1. 两数之和

1. 题目链接LeetCode1. 两数之和
2. 学习资料:
3. 思路
  1. 本题重点:
    • 为什么会想到用哈希表?
    • 哈希表为什么用map?
    • 本题map是用来存什么?
    • map中的key和value用来存什么的?
  2. 对1的回答:
    • 每当让我们判断某个元素是否出现过或是判断某个元素是否在集合中出现过。
    • 因为我们要存放元素和元素所在的数组下标,因此我们要使用map。
    • map是用来存放我们遍历过的元素。
    • 我们要查找的是元素是否出现过因此key用来存元素,value用来存元素所在的数组下标。
4. 代码
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 t = target - nums[i];
            if(map.containsKey(t)){
                res[0] = map.get(t);
                res[1] = i;
            }else{
                map.put(nums[i],i);
            }
        }
        return res;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
第二十二天的算法训练营主要涵盖了Leetcode题目中的三道题目,分别是Leetcode 28 "Find the Index of the First Occurrence in a String",Leetcode 977 "有序数组的平方",和Leetcode 209 "长度最小的子数组"。 首先是Leetcode 28题,题目要求在给定的字符串中找到第一个出现的字符的索引。思路是使用双指针来遍历字符串,一个指向字符串的开头,另一个指向字符串的结尾。通过比较两个指针所指向的字符是否相等来判断是否找到了第一个出现的字符。具体实现的代码如下: ```python def findIndex(self, s: str) -> int: left = 0 right = len(s) - 1 while left <= right: if s[left == s[right]: return left left += 1 right -= 1 return -1 ``` 接下来是Leetcode 977题,题目要求对给定的有序数组中的元素进行平方,并按照非递减的顺序返回结果。这里由于数组已经是有序的,所以可以使用双指针的方法来解决问题。一个指针指向数组的开头,另一个指针指向数组的末尾。通过比较两个指针所指向的元素的绝对值的大小来确定哪个元素的平方应该放在结果数组的末尾。具体实现的代码如下: ```python def sortedSquares(self, nums: List[int]) -> List[int]: left = 0 right = len(nums) - 1 ans = [] while left <= right: if abs(nums[left]) >= abs(nums[right]): ans.append(nums[left ** 2) left += 1 else: ans.append(nums[right ** 2) right -= 1 return ans[::-1] ``` 最后是Leetcode 209题,题目要求在给定的数组中找到长度最小的子数组
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值