Bootstrap

Leetcode DAY7: 四数相加II and 赎金信 and 三数之和 and 四数之和

454.四数相加II

class Solution:
    def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int: 
        count = 0
        hashmap = dict()
        for n1 in nums1:
            for n2 in nums2:
                if n1 + n2 in hashmap:
                    hashmap[n1 + n2] += 1
                else:
                    hashmap[n1 + n2] = 1
        for n3 in nums3:
            for n4 in nums4:
                if -(n3 + n4) in hashmap:
                    count += hashmap[-(n3 + n4)]
        return count
先把n1、n2的值加起来 放进哈希表中 再把那在哈希表中 找-(n3+n4)的值出现的次数 也就是要得到的元组的个数

383. 赎金信

(1)用数组作为哈希表

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        map = [0] * 26
        for m in magazine:
            map[ord(m)-ord("a")] += 1
        for r in ransomNote:
            map[ord(r)-ord("a")] -= 1
        for m in map:
            if m < 0:
                return False
        return True

(2)用python中的collection.Counter()函数

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        r = collections.Counter(ransomNote)
        m = collections.Counter(magazine)
        x = r - m
        if len(x) > 0:
            return False
        else:
            return True

注:字典类型也可以相加减,用collections.Counter(dict)实现,但结果会自动舍掉value值<=0的dict.items()

15. 三数之和

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        res = []
        nums.sort()
        for i in range(len(nums)):
            left = i + 1
            right = len(nums) - 1
            if nums[i] > 0:
                break
            if i >= 1 and nums[i] == nums[i - 1]:
                continue

            while left < right:
                total = nums[i] + nums[left] + nums[right]
                if total > 0:
                    right -= 1
                elif total < 0:
                    left += 1
                else:
                    res.append([nums[i], 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

用双指针的方法  寻找nums[i] + nums[left] + nums[right] == 0

重点是去重的逻辑 需要考虑的是 nums[i]、nums[left]、nums[right]这三个的重复

nums[i]的去重逻辑是 寻找nums[i] ?= nums[i - 1]

nums[left]、nums[right]的去重逻辑应该放在 找到一个三元组之后

18. 四数之和

class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        res = []
        n = len(nums)
        nums.sort()
        for i in range(n):
            if i > 0 and nums[i] == nums[i - 1]:continue
            for j in range(i + 1, n):
                if j > i + 1 and nums[j] == nums[j - 1]:continue
                left = j + 1
                right = n - 1
                while left < right:
                    if nums[i] + nums[j] + nums[left] + nums[right] > target:
                        right -= 1
                    elif nums[i] + nums[j] + nums[left] + nums[right] < target:
                        left += 1
                    else:
                        res.append([nums[i], nums[j], 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 

理解了三数之和  这个就多一个循环就好了 比较好理解 去重逻辑是一样的 

;