• day14【代码随想录】有效字母的异位词、赎金信、两个数组的交集、两个数组的交集||



    一、有效字母的异位词(力扣242)

    给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
    注意:若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。

    在这里插入图片描述

    1、哈希表

    定义一个数组叫做record用来上记录字符串s里字符出现的次数。

    需要把字符映射到数组也就是哈希表的索引下标上,字符a映射为下标0,相应的字符z映射为下标25。

    再遍历 字符串s的时候,只需要将 s[i] - ‘a’ 所在的元素做+1 操作即可,这样就将字符串s中字符出现的次数,统计出来了。
    同样在遍历字符串t的时候,对t中出现的字符映射哈希表索引上的数值再做-1的操作。

    那么最后,record数组如果有的元素不为零0,说明字符串s和t一定是谁多了字符或者谁少了字符,return false。
    最后如果record数组所有元素都为零0,说明字符串s和t是字母异位词,return true。

    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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这里插入图片描述

    2、直接排序然后判断是否相等

    思路:判断规则是两个字符串中所有字符的出现次数是否相同,那么排序后遍历即可得出结论

    class Solution {
        public boolean isAnagram(String s, String t) {
            // 判空
            if(s == null || t == null || s.length() != t.length()){
                return false;
            }
            char[] sCharArray = s.toCharArray();
            char[] tCharArray = t.toCharArray();
            Arrays.sort(sCharArray);
            Arrays.sort(tCharArray);
            for(int i = 0; i < sCharArray.length; i++){
                if(sCharArray[i] != tCharArray[i]){
                    return false;
                }
            }
            return true;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    二、赎金信(力扣383)

    给你两个字符串:ransomNote 和 magazine ,判断 ransomNote 能不能由 magazine 里面的字符构成。
    如果可以,返回 true ;否则返回 false 。
    magazine 中的每个字符只能在 ransomNote 中使用一次。
    在这里插入图片描述

    与有效字母异位词基本一致 除了最后的判断条件需要修改为当值只要大于0就需要返回false

    class Solution {
        public boolean canConstruct(String ransomNote, String magazine) {
            int[] record = new int[26];
    
            for(int i=0;i<ransomNote.length();i++){
                record[ransomNote.charAt(i)-'a']++;
            }
    
            for(int i=0;i<magazine.length();i++){
                record[magazine.charAt(i)-'a']--;
            }
    
            for(int count:record){
                if(count>0){
                    return false;
                }
            }
            return true;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在这里插入图片描述

    三、两个数组的交集(力扣359)

    给定两个数组 nums1 和 nums2 ,返回 它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序
    在这里插入图片描述

    1、暴力求解

    用两个数组来记录每个字符串中出现字符
    当出现都不等于0的情况下 就是两个字符串中共同出现的字符

    class Solution {
        public int[] intersection(int[] nums1, int[] nums2) {
            int[] record1 = new int[1000];
            int[] record2 = new int[1000];
    
    
            int[] result = new int[1000];
            int k=0;
            for(int i=0;i<nums1.length;i++){
                record1[nums1[i]]++;
            }
    
            for(int i=0;i<nums2.length;i++){
                record2[nums2[i]]++;
            }
    
            for(int i=0;i<1000;i++){
                if(record1[i]!=0&&record2[i]!=0){
                    result[k++]=i;
                }
            }
            int[] res = new int[k];
            for(int i=0;i<k;i++){
                res[i]=result[i];
            }
            return res; 
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    在这里插入图片描述

    2、Set集合

    当案例为 0 5 10000 浪费较多空间
    这种情况下使用set比较合适

    class Solution {
        public int[] intersection(int[] nums1, int[] nums2) {
            //两个set数组
            Set<Integer> set1 = new HashSet<Integer>();
            Set<Integer> set2 = new HashSet<Integer>();
    
            //存放数据
            for(int num:nums1){
                set1.add(num);
            }
            for(int num:nums2){
                set2.add(num);
            }
            return getIntersection(set1,set2);
        }
    
    
    
        public int[] getIntersection(Set<Integer> set1,Set<Integer> set2){
            if(set1.size()>set2.size()){
                return getIntersection(set2,set1);
            }
            Set<Integer> intersectionSet =new HashSet<Integer>();
            for(int num:set1){
                if(set2.contains(num)){
                    intersectionSet.add(num);
                }
            }
    
            int[] intersection = new int[intersectionSet.size()];
            int index=0;
            for(int num:intersectionSet){
                intersection[index++]=num;
            }
            return intersection;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    在这里插入图片描述

    四、两个数组的交集||(力扣350)

    给你两个整数数组 nums1 和 nums2 ,请你以数组形式返回两数组的交集。返回结果中每个元素出现的次数,应与元素在两个数组中都出现的次数一致(如果出现次数不一致,则考虑取较小值)。可以不考虑输出结果的顺序。

    在这里插入图片描述
    与第三题相比 差别在于不需要去重
    此时就需要记录 字符出现的次数
    遍历nums2数组 与记录中的字符相比较 如果相同 则需要将对应map集合的字符出现次数进行- -

    class Solution {
        public int[] intersect(int[] nums1, int[] nums2) {
            if(nums1.length>nums2.length){
                return intersect(nums2,nums1);
            }
            Map<Integer,Integer> map = new HashMap<Integer,Integer>();
            //这个map集合里边存放nums1数组里边的字符以及对应出现的次数
            for(int num:nums1){
                int count = map.getOrDefault(num,0)+1;
                map.put(num,count);
            }
            int[] intersection = new int[nums1.length];
            int index = 0;
            for(int num:nums2){
                int count = map.getOrDefault(num,0);
                if(count>0){
                    intersection[index++]=num;
                    count--;
                    if(count>0){
                        map.put(num,count);
                    }else{
                        map.remove(num);
                    }
                }
            }
            return Arrays.copyOfRange(intersection,0,index);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
  • 相关阅读:
    文件服务器审核
    场效应管(MOSFET)笔记-nmos和pmos仿真测试
    centos7安装mysql8.0.31
    机器学习-搜索技术:从技术发展到应用实战的全面指南
    迎接金九银十的狂风暴雨,最强Java面经八股文,跳槽必备。
    随手记面试录
    2022.8.8好题选讲(数论场)
    数据库基本操作
    lampiao靶场
    python基于django的汽车租赁系统nodejs+vue+element
  • 原文地址:https://blog.csdn.net/qq_42338744/article/details/128176155