【代码随想录训练营】【Day 7】【哈希表-2】| Leetcode 454, 383, 15, 18

本文讨论了如何使用哈希表解决LeetCode中的四数之和问题(454,383,15,18),重点在于去重策略,包括对数组元素的去重和在寻找和的过程中对left和right指针的处理。
摘要由CSDN通过智能技术生成

【代码随想录训练营】【Day 7】【哈希表-2】| Leetcode 454, 383, 15, 18

需强化知识点

  • 四数之和,去重细节

题目

454. 四数相加 II

  • 注意两两拆分思想
class Solution:
    def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
        hashdict = {}
        for n1 in nums1:
            for n2 in nums2:
                hashdict[n1 + n2] = hashdict.get(n1+n2, 0) + 1
        
        result = 0
        for n3 in nums3:
            for n4 in nums4:
                key = - n3 - n4
                if key in hashdict:
                    result += hashdict[key]

        return result

383. 赎金信

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        r_dict = {}
        m_dict = {}

        for c in ransomNote:
            r_dict[c] = r_dict.get(c, 0) + 1
        
        for c in magazine:
            m_dict[c] = m_dict.get(c, 0) + 1
        
        for key, value in r_dict.items():
            if m_dict.get(key, 0) < value:
                return False
        return True

15. 三数之和

  • 注意对a的去重,以及找到一组答案后,对left和right的去重,需要再执行一次加减操作
class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        result = []
        n = len(nums)
        nums.sort()

        for i in range(n-2):
            if i > 0 and nums[i] == nums[i-1]:
                continue
            if nums[i] > 0:
                return result
            left, right = i + 1, n-1
            while left < right:
                sum_ = nums[i] + nums[left] + nums[right]
                if sum_ == 0:
                    result.append([nums[i], nums[left], nums[right]])
                    while left<right and nums[left + 1] == nums[left]:
                        left += 1
                    while right>left and nums[right - 1] == nums[right]:
                        right -= 1
                    left += 1
                    right -= 1
                elif sum_ > 0:
                    right -= 1
                else:
                    left += 1
        return result
      

18. 四数之和

  • 注意对a的去重,以及找到一组答案后,对left和right的去重,需要再执行一次加减操作
  • 注意和三数之和的区别,此处是target,对a和b的去重,需要满足if nums[i] > target and nums[i] > 0 and target > 0条件,因为如果target为负数,尽管nums[i] > target,但后续多个负数相加,仍能满足
  • 其次,对于b的去重,不能直接return,只能break 跳出此次循环
class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        result = []
        n = len(nums)
        nums.sort()

        for i in range(n-3):
            if nums[i] > target and target > 0:
                return result
            if i > 0 and nums[i] == nums[i-1]:
                continue
            
            for j in range(i+1, n-2):
                if nums[i] + nums[j] > target and target > 0:
                    # return result
                    break
                if j > i+1 and nums[j] == nums[j-1]:
                    continue
                
                left, right = j+1, n-1
                while left < right:
                    sum_ = nums[i] + nums[j] + nums[left] + nums[right]

                    if sum_ == target:
                        result.append([nums[i], nums[j], nums[left], nums[right]])

                        while left < right and nums[left + 1] == nums[left]:
                            left += 1
                        left += 1
                        while left < right and nums[right - 1] == nums[right]:
                            right -= 1
                        right -= 1
                    elif sum_ > target:
                        right -= 1
                    else:
                        left += 1
        return result
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值