代码随想录算法训练营Day 07|哈希表Part02|454.四数相加II、 383. 赎金信、 15. 三数之和、 18. 四数之和

代码随想录算法训练营Day 07|哈希表Part02|454.四数相加II、 383. 赎金信、 15. 三数之和、 18. 四数之和



454.四数相加II

题目链接

一、defaultdict(map)保存

class Solution(object):
    def fourSumCount(self, nums1, nums2, nums3, nums4):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :type nums3: List[int]
        :type nums4: List[int]
        :rtype: int
        """
        record = collections.defaultdict(int)
        count = 0
        for i in nums1:
            for j in nums2:
                record[i+j] +=1
        for i in nums3:
            for j in nums4:
                target = 0-(i+j)
                if target in record:
                    count += record[target]  # 注意此处count +的值
        return count

二、本题总结

两层for循环搞定,感觉几数相加这种题都不免要使用到多层for循环,大胆用!


383. 赎金信

题目链接

一、defaultdict

class Solution(object):
    def canConstruct(self, ransomNote, magazine):
        """
        :type ransomNote: str
        :type magazine: str
        :rtype: bool
        """
        record =[0]*26
        
        for i in range(len(magazine)):
            record[ord(magazine[i])-ord('a')] +=1
        for i in range(len(ransomNote)):
            record[ord(ransomNote[i])-ord('a')] -=1
        for i in range(len(record)):
            if record[i]<0:
                return False
        return True

二、本题总结

和有效字母异位词很像很像的一道题,也可以用数组再写写.


15. 三数之和

题目链接

一、双指针法

class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        # 双指针法
        nums=sorted(nums)
        result = []
        for i in range(len(nums)):
            if nums[i]>0:
                return result
            if i>0 and nums[i]==nums[i-1]:
                continue
            left = i+1
            right = len(nums)-1
            while(left<right):
                if nums[i]+nums[left]+nums[right]<0:
                    left += 1
                elif nums[i]+nums[left]+nums[right]>0:
                    right -= 1
                else:
                    result.append([nums[i],nums[left],nums[right]])
                    while left<right and nums[right] == nums[right-1]:
                        right -= 1
                    while left<right and nums[left] == nums[left+1]:
                        left += 1
                    right -= 1
                    left += 1
        return result

二、哈希表解法

留下之后写 但是大部分解法还是双指针

三、本题总结

双指针+剪枝操作

18. 四数之和

题目链接

一、双指针法

class Solution(object):
    def fourSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        result = []
        nums.sort()
        for i in range(len(nums)):
            if nums[i]>0 and nums[i]>target:
                break
            if i>0 and nums[i] == nums[i-1]:
                continue
            for j in range(i+1,len(nums)):
                if nums[i]+ nums[j]>0 and nums[i]+nums[j]>target:
                    break
                if j>i+1 and nums[j] == nums[j-1]:
                    continue
                left = j+1
                right = len(nums)-1
                while(left<right):
                    sum = nums[i]+nums[j]+nums[left]+nums[right]
                    if sum>target:
                        right -=1
                    elif sum <target:
                        left +=1
                    else:
                        result.append([nums[i],nums[j],nums[left],nums[right]])
                        while left<right and nums[right] == nums[right-1]:
                            right -= 1
                        while left<right and nums[left] == nums[left+1]:
                            left +=1
                        right -=1
                        left +=1
        return result

二、本题总结

属于是三数之和的变形

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值