LeetCode每日十题---哈希表(四)

题目描述1

在这里插入图片描述

笔者解答1.1

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
     List<List<String>> IList=new ArrayList<List<String>>();
     Map<Map,Integer> IMap=new HashMap<Map,Integer>();
     int n=strs.length;
     int i;
     int index=0;
     for(i=0;i<n;i++){
         Map<Character,Integer> map=new HashMap<>();
         for(int j=0;j<strs[i].length();j++){
           map.put(strs[i].charAt(j),map.getOrDefault(strs[i].charAt(j),0)+1);
         }
         if(i==0){
             IMap.put(map,index);
             List<String> list=new ArrayList<String>();
             list.add(strs[i]);
             IList.add(list);
         }else{
             if(IMap.containsKey(map)){
                 IList.get(IMap.get(map)).add(strs[i]);
             }else{
                 index++;
                 IMap.put(map,index);
                 List<String> list=new ArrayList<String>();
                 list.add(strs[i]);
                 IList.add(list);      
             }
         }
     }
     return IList;
    }
}

笔者分析1.2

这次有点秀操作,虽然通过了但效率有点低,不过评论区的大佬操作是更六

class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> res = new ArrayList<List<String>>();
        Map<String, List<String>> map = new HashMap<>();//这个哈希表用的比我还过分啊
        for(int i = 0; i < strs.length; i++){
            char[] arr = strs[i].toCharArray();//
            Arrays.sort(arr);//
            String s = new String(arr);//就这三步就被秀了一脸
            if(!map.containsKey(s)){
                List<String> temp = new ArrayList<>();
                temp.add(strs[i]);
                map.put(s,temp);
            }else{
                map.get(s).add(strs[i]);
            }
        }
        for(String key: map.keySet()){
            res.add(map.get(key));
        }
        return res;
    }
}

题目描述2

在这里插入图片描述

笔者分析2.1

原本考虑用哈希表的,但时间复杂度太高了。评论区的递推还是挺不错的,二维数组牛逼!

class Solution {
    public int findLength(int[] A, int[] B) {
        if (A.length == 0 || B.length == 0) {
            return 0;
        }
        int[][] dp = new int[A.length+1][B.length+1];
        int result = 0;
        for (int i = 1; i <= A.length; i++) {
            for (int j = 1; j <= B.length; j++) {
                if (A[i-1] == B[j-1]) {
                    dp[i][j] = dp[i-1][j-1]+1;
                    result = Math.max(result, dp[i][j]);
                }
            }
        }
        return result;
    }
}

题目描述3

在这里插入图片描述

笔者分析3.1

再次栽在下面这个方法,真巧妙,哈希表的精髓啊,一直求和,找差值出现的次数就行。以后求一组序列中,通过某种运算得一定值,问连续子数组次数的,就用哈希差值法。(自己编的名)

class Solution {
    public int subarraySum(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        map.put(0, 1);
        int sum = 0, ret = 0;
        for(int i = 0; i < nums.length; ++i) {
            sum += nums[i];
            if(map.containsKey(sum-k))
                ret += map.get(sum-k);
            map.put(sum, map.getOrDefault(sum, 0)+1);
        }      
        return ret;
    }
}
class Solution{
   public int subarraySum(int[] nums,int k){
      Map<Integer,Integer> map=new HashMap<>();
      map.put(0,1);
      int sum=0,ret=0;
      for(int i=0;i<nums.length;i++){
         sum+=nums[i];
         if(map.containsKey(sum-k))
              ret+=map.get(sum-k);
           map.put(sum,map.getOrDefault(sum,0)+1);
      }
      return ret;
   }
}

总结

学到了一个哈希差值法,每日十题第二十一天,以下图为证
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路的苟狗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值