当前位置:   article > 正文

代码随想录算法训练营Day7|哈希2

代码随想录算法训练营Day7|哈希2

454.四数相加II[中等题]

  • 四个独立数组分为两两一组,就跟两数之和一样了
class Solution:
    def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
        import collections
        hashmap = collections.defaultdict(int)
        for i in nums1:
            for j in nums2:
                hashmap[i+j] += 1
        res = 0
        for i in nums3:
            for j in nums4:
                key = -i-j
                if key in hashmap.keys():
                    res += hashmap[key]
        return res
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

383. 赎金信 [简单题]

  • 常规简单题
class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        hashmap = [0]*26
        for i in magazine:
            hashmap[ord(i)-97] += 1
        for i in ransomNote:
            hashmap[ord(i)-97] -= 1
            if hashmap[ord(i)-97]<0:
                return False
        return True
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

15.三数之和[中等题]

  • 返回值,需要去重,哈希方法可考虑利用set去重
  • 用哈希但不用set的去重逻辑有点难,考虑左右双指针
class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        nums.sort()
        N = len(nums)
        res = []
        for ind,target in enumerate(nums):
            if target>0:
                return res
            if ind>0 and target == nums[ind-1]:      ## ind>0是需要把[0,0,0]包含进来 第一个数去重
                continue

            left,right = ind+1,N-1
            while(left<right):

                total = nums[left] +nums[right]
                if total > -target:
                    right -= 1
                elif total < -target:
                    left += 1
                else:
                    res.append([target,nums[left],nums[right]])
                    while left< right and nums[left]==nums[left+1]:
                        left += 1
                    while left< right and nums[right]==nums[right-1]:
                        right -= 1
                    left+=1
                    right-=1
        return res
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

18.四数之和[中等题]

  • 在三数之和的基础之上增加一个for循环,如果是五数之和,再增加一个for循环
  • 由于target可能是负数,因此for后的剪枝要判断target>0才能剪枝。因为是负数存在的情况下,往后叠加也可以更小。
class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        res = []
        nums.sort()
        n = len(nums)
        for i in range(n-3):
            if i>0 and nums[i]==nums[i-1]:
                continue
            for j in range(i+1,n-2):
                if j>i+1 and nums[j]==nums[j-1]:
                    continue
                left,right = j+1,n-1
                while(right>left):
                    temp = target-(nums[i]+nums[j])
                    if nums[left]+nums[right]>temp:
                        right -= 1
                    elif nums[left]+nums[right]<temp:
                        left += 1
                    else:
                        res.append([nums[i],nums[j],nums[left],nums[right]])

                        while(left<right and nums[left+1]==nums[left]):
                            left += 1
                        while(left<right and nums[right-1]==nums[right]):
                            right -= 1
                        left += 1
                        right -= 1
        return res
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/494579
推荐阅读
相关标签
  

闽ICP备14008679号