代码随想录算法训练营day7|

文章介绍了如何使用哈希表优化四数相加II问题,将时间复杂度从n的四次方降低到两个n的平方。同时,对于三数之和问题,提出了使用排序和双指针的方法避免暴力求解,提高效率。四数之和问题则是三数之和的扩展,同样应用了剪枝优化。最后,赎金信问题通过简单的计数解决字符串包含性问题。

454.四数相加II

本题中四个数分别来自不同的数组,按照暴力算法四四组合,时间复杂度为n的四次方,果断放弃思考更优解法。

思来想去,要判断和是否为目标值,也可以理解为找出两个不同数组元素的和恰好为目标值和另外两个不同数组元素的差值,转换到了哈希问题:判断差值是否出现在前两个数组元素和的集合中。

此时有两种选择,hashset或者hashmap,但是本题中元素和会存在重复出现的可能性,因此我们要选择hashmap让和对应出现次数这个值。

按照这个思路,将n的四次方问题转换成了两个n的平方问题。

class Solution {
    public int  fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
        int count=0;
        Map<Integer,Integer> mat=new HashMap<Integer,Integer>();
         for(int i=0;i<nums1.length;i++){
                    for(int j=0;j<nums2.length;j++){
                        mat.put(nums1[i]+nums2[j],mat.getOrDefault(nums1[i]+nums2[j],0)+1);
                    }
                }
        for(int i=0;i<nums3.length;i++){
            for(int j=0;j<nums4.length;j++){
                if(mat.containsKey(-(nums3[i]+nums4[j])))
                    count+=mat.get(-(nums3[i]+nums4[j]));
            }
        }
        return count;
    }
}

15.三数之和

我觉得本题其实跟哈希法关联并不大,反而双指针更好理解,也更好写。

首先,这个题目只要求输出和为0的数,不要求写出下标,而且不能出现重复的组合。倘若我们在找出所有组合后再去去重,在找出所有组合这个问题上,由于数组元素是无序的,只能暴力出来。暴力算法时间复杂度已经足够大了还要查重,因此必须要“投机取巧”了。

在数组的问题上有一个经典而且好用的方法,双指针。要使用双指针法的前提是能够把握住指针滑动的方向和条件。在本题中,我们可以先将数组排序,先固定一个加数(当然应该先选择最左边),那么这个时候,问题就转化成了找出两个数组元素的和为固定加数和目标值的差。双指针在这个时候就派上了用场,由于我们做了排序处理,当我们将指针放置于一头一尾时,这时候两数组元素和只会出现相等大于或者小于情况,小于目标值时,头指针往右移(递增数组) ,反之则·····。

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> threeSum=new ArrayList<List<Integer>>();
        if(nums.length<3)
        return threeSum;
        Arrays.sort(nums);
        int first=0;
        int n=nums.length;
        int third=n-1;
        while(first<n-2){
            if(first>0&&nums[first]==nums[first-1]){
                ++first;
                continue;
            }
            third=n-1;
            int second=first+1;
            while(second<third){
                if(second>first+1&&nums[second]==nums[second-1]){
                    ++second;
                    continue;
                }
                while(nums[first]+nums[second]+nums[third]>0&&third>second+1)
                    --third;
                if(nums[first]+nums[second]+nums[third]==0){
                    List<Integer> list=new ArrayList<Integer>();
                    list.add(nums[first]);
                    list.add(nums[second]);
                    list.add(nums[third]);
                    threeSum.add(list);
                }
                ++second;
            }
            ++first;
            
        }
        return threeSum;
    }
}

18.四数之和

这题在我看来就是套三数之和的娃,再进行剪枝的优化处理。

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> fourSum=new ArrayList<List<Integer>>();
        long sum;
        //获取数据长度
        int n=nums.length;
        //排序,避免重复
        Arrays.sort(nums);
        for(int first=0;first<n-3;first++){
            //判断与前一元素是否相同,避免重复
            if(nums[first]>0&&nums[first]>=target)
                break;
            if(first>0&&nums[first]==nums[first-1])
                continue;
            
            
            for(int second=first+1;second<n-2;second++){
                if((long)nums[first]+nums[second]>0&&(long)nums[first]+nums[second]>=target) break;
                if(second>first+1&&nums[second]==nums[second-1])
                    continue;
             int fourth=n-1;   
            for(int third=second+1;third<fourth;third++){
                    if(third>second+1&&nums[third]==nums[third-1])
                        continue;
                
                sum=0;
                sum=(long)nums[first]+nums[second]+nums[third];
               
                while((long)sum+nums[fourth]>target&&fourth>third+1){
                    --fourth;
                }
                    
                if((long)sum+nums[fourth]==target){
                    List<Integer> list=new ArrayList<Integer>();
                    list.add(nums[first]);
                    list.add(nums[second]);
                    list.add(nums[third]);
                    list.add(nums[fourth]);
                    fourSum.add(list);
                }

            }
            }
        }
        return fourSum;

    }
}

383.赎金信

这题真的是今天最轻松的了,简直就是饭后甜点级别。

class Solution {
    public boolean canConstruct(String ransomNote, String magazine) {
         int[] a=new int[26];
         for(int i=0;i<magazine.length();i++){
             int index= magazine.charAt(i)-97;
             a[index]++;
         }
         for(int i=0;i<ransomNote.length();i++){
             int index=ransomNote.charAt(i)-97;
             a[index]--;
             if(a[index]<0)
             return false;
         }
         return true;



    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值