代码随想录第6天||454四数相加、383赎金信、15三数之和、18四数之和

454四数相加

链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

  • /*
    把四个数组分成两组,然后遍历这两组数组,把值相加,分别存到两个HashMap中。
    key代表某两个数的和,value存有多少个组合能得到这个结果。
    然后遍历其中一个hashMap的key,如果另一个hashMap中有能满足要求的key,就把它们对应的value相乘,加到sum上。
    */
    class Solution {
        public int fourSumCount(int[] nums1, int[] nums2, int[] nums3, int[] nums4) {
            Map<Integer,Integer> map1= new HashMap<>();
            Map<Integer,Integer> map2= new HashMap<>();
            int len=nums1.length;
    
            //用两个HashMap存
            for(int i=0;i<len;i++){
                    for(int j=0;j<len;j++){
                        if(map1.containsKey(nums1[i]+nums2[j])){
                            map1.put(nums1[i]+nums2[j],map1.get(nums1[i]+nums2[j])+1);}else{
                                 map1.put(nums1[i]+nums2[j],1);
                            }
                    
                        if(map2.containsKey(nums3[i]+nums4[j])){
                            map2.put(nums3[i]+nums4[j],map2.get(nums3[i]+nums4[j])+1);}
                    else{
                        map2.put(nums3[i]+nums4[j],1);
                    }
                    }
            }
            int num=0;
            Set<Map.Entry<Integer,Integer>> set1=map1.entrySet();
            for(Map.Entry<Integer,Integer> entry:set1){
                if(map2.keySet().contains(-entry.getKey())){
                    num+=map2.get(-entry.getKey())*entry.getValue();
                }
            }
    
            return num;
        }
    }

看了一下题解也是我这样做的。


383赎金信

题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

  • //和之前有个题的思路一样,都是用数组结构的哈希来做,要注意下两个字符串的长度。。。。
    class Solution {
        public boolean canConstruct(String ransomNote, String magazine) {
            //去magzine里找randomNote
            int[] res=new int[27];
            if(magazine.length()<ransomNote.length()){
                return false;}
            for(int i=0;i<magazine.length();i++){
                    res[magazine.charAt(i)-'a']++;
                    if(i<ransomNote.length()){
                    res[ransomNote.charAt(i)-'a']--;
                    }
            }
            for(int i=0;i<res.length;i++){
                if (res[i]<0){
                    return false;
                }
            }
            return true;
        }
    }

    三数之和

  • 链接力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台
//用循环做了一遍。。。超时了TAT。。。。
class Solution {
    public List<List<Integer>> threeSum(int[] nums) {

        //Set<List> set=new HashSet<>();//临时结果
        Arrays.sort(nums);
        int i=0;
        int s=1;
        int f=2;
        Set<List<Integer>> res=new HashSet<>();
        for(i=0;i<nums.length-2;i++){
            while(i<nums.length-1&i>0&&nums[i]==nums[i-1]){i++;}
            s=i+1;
            while(s<nums.length-1){
                f=s+1;
                while(nums[i]+nums[f]+nums[s]<0){
                    if(f<nums.length-1){f++;
                    }else{
                        break;}
                }
                List<Integer> temp=new ArrayList<>();
                if(nums[i]+nums[f]+nums[s]==0){
                    temp.add(nums[i]);
                    temp.add(nums[s]);
                    temp.add(nums[f]);
                    res.add(temp);
                }
                s++;
            }

        }
        List<List<Integer>> ress=new ArrayList();
        for(List<Integer> t:res){
            ress.add(t);
        }
return ress;
        
        }

}
            
//看了题解做了一遍

class Solution {
    public List<List<Integer>> threeSum(int[] nums) {
        Arrays.sort(nums);
        List<Integer> temp=new ArrayList<>();
        List<List<Integer>> res=new ArrayList();
        for(int i=0;i<nums.length;i++){



            if(i>0&&nums[i]==nums[i-1]){continue;}
            int l=i+1;
            int r=nums.length-1;
            while(l<r){
                if(nums[i]+nums[l]+nums[r]==0){
                    temp=new ArrayList<>();
                    temp.add(nums[i]);
                    temp.add(nums[l]);
                    temp.add(nums[r]);
                    res.add(temp);
            while(l<r&&nums[l]==nums[l+1]){
            l++;
            }
            while(l<r&&nums[r]==nums[r-1]){
                r--;
            }   l++;
                r--;
                }else if(nums[i]+nums[l]+nums[r]>0){
                    r--;
                }else if(nums[i]+nums[l]+nums[r]<0){
                    l++;

            }
                }

        }
        return res;
        }

}
            
  • 我把题解也放上来,他的注释写的比较详细。
  • class Solution {
        //定义三个指针,保证遍历数组中的每一个结果
        //画图,解答
        public List<List<Integer>> threeSum(int[] nums) {
            //定义一个结果集
            List<List<Integer>> res = new ArrayList<>();
            //数组的长度
            int len = nums.length;
            //当前数组的长度为空,或者长度小于3时,直接退出
            if(nums == null || len <3){
                return res;
            }
            //将数组进行排序
            Arrays.sort(nums);
            //遍历数组中的每一个元素
            for(int i = 0; i<len;i++){
                //如果遍历的起始元素大于0,就直接退出
                //原因,此时数组为有序的数组,最小的数都大于0了,三数之和肯定大于0
                if(nums[i]>0){
                    break;
                }
                //去重,当起始的值等于前一个元素,那么得到的结果将会和前一次相同
                if(i > 0 && nums[i] == nums[i-1]) continue;
                int l = i +1;
                int r = len-1;
                //当 l 不等于 r时就继续遍历
                while(l<r){
                    //将三数进行相加
                    int sum = nums[i] + nums[l] + nums[r];
                    //如果等于0,将结果对应的索引位置的值加入结果集中
                    if(sum==0){
                        // 将三数的结果集加入到结果集中
                        res.add(Arrays.asList(nums[i], nums[l], nums[r]));
                        //在将左指针和右指针移动的时候,先对左右指针的值,进行判断
                        //如果重复,直接跳过。
                        //去重,因为 i 不变,当此时 l取的数的值与前一个数相同,所以不用在计算,直接跳
                        while(l < r && nums[l] == nums[l+1]) {
                            l++;
                        }
                        //去重,因为 i不变,当此时 r 取的数的值与前一个相同,所以不用在计算
                        while(l< r && nums[r] == nums[r-1]){
                            r--;
                        } 
                        //将 左指针右移,将右指针左移。
                        l++;
                        r--;
                        //如果结果小于0,将左指针右移
                    }else if(sum < 0){
                        l++;
                        //如果结果大于0,将右指针左移
                    }else if(sum > 0){
                        r--;
                    }
                }
            }
            return res;
        }
    }

四数之和

链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台

  • 代码:
  • class Solution {
        public List<List<Integer>> fourSum(int[] nums, int target) {
            Arrays.sort(nums);
            List<List<Integer>> res=new ArrayList();        
            for(int i=0;i<nums.length-3;i++){
                    //if(nums[0]>target||nums.length<4) return new ArrayList<>();
                    if(i>0&&nums[i]==nums[i-1]){
                        continue;
                    }
                long target0=target-nums[i];
                int start=i+1;;
                for(int t=start;t<nums.length-2;t++){
                    int left=t+1;
                    int right=nums.length-1;
                    //if(nums[t]>target0){break;}
                        if(t>i+1&&nums[t]==nums[t-1]){
                            continue;
                        }
                    while(right>left){
                        long sum=(long)nums[right]+nums[left]+nums[t];
                        if(sum==target0){
                            ArrayList<Integer> temp=new ArrayList<Integer>();
                            temp.add(nums[i]);
                            temp.add(nums[t]);
                            temp.add(nums[left]);
                            temp.add(nums[right]);
                            res.add(temp);
                            while(left<right&&nums[left]==nums[left+1]){
                                    left++;
                                }
                            while (left<right&&nums[right]==nums[right-1]){
                                right--;
                                }
                                left++;
                                right--;
                            
                        }else{
                            if(sum>target0){
                                right--;
    
                            }else  if(sum<target0){
                                    left++;
                            }
                        }
                    }
                }
            }
            return res;
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值