代码随想录算法训练营第七天| 454.四数相加II、383. 赎金信、15. 三数之和、18. 四数之和

54 阅读2分钟

Leetcode 454 4Sum II

1. 第一想法

4个也太多了,不知道怎么处理。

2. 看完后想法

class Solution:
    def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
        hashmap = dict()
        for n1 in nums1:
            for n2 in nums2:
                if n1 + n2 in hashmap:
                    hashmap[n1+n2] += 1
                else:
                    hashmap[n1+n2] = 1
        count = 0
        for n3 in nums3:
            for n4 in nums4:
                key = - n3 - n4
                if key in hashmap:
                    count += hashmap[key]
        return count

3. 总结

先预处理前两个数组,再处理后两个,再去查是否有想要的元素,应想到哈希法。key 放数值,value 放出现的次数。

Leetcode 383 Ransom Note

1. 第一想法

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        occur = dict()
        for s in ransomNote:
            if s in occur:
                occur[s] += 1
            else:
                occur[s] = 1
        for s in magazine:
            if s in occur:
                if occur[s] > 0:
                    occur[s] -= 1
                    if occur[s] == 0:
                        del occur[s]
                else:
                    del occur[s]
        if occur == {}:
            return True
        else:
            return False

2. 看完后想法

这个情况确实用数组会节省一些空间。

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:
        ransom_count = [0] * 26
        magazine_count = [0] * 26
        for c in ransomNote:
            ransom_count[ord(c) - ord('a')] += 1
        for c in magazine:
            magazine_count[ord(c) - ord('a')] += 1
        return all(ransom_count[i] <= magazine_count[i] for i in range(26))

3. 总结

其实我都没注意到题目里说只有小写字母。

Leetcode 15 3Sum

1. 第一想法

没什么头绪。

2. 看完后想法

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        result = []
        nums.sort()
        
        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 right > left:
                sum_ = nums[i] + nums[left] + nums[right]
                
                if sum_ < 0:
                    left += 1
                elif sum_ > 0:
                    right -= 1
                else:
                    result.append([nums[i], nums[left], nums[right]])
                    
                    while right > left and nums[right] == nums[right - 1]:
                        right -= 1
                    while right > left and nums[left] == nums[left + 1]:
                        left += 1
                        
                    right -= 1
                    left += 1
        return result

3. 总结

常看常新。

Leetcode 18 4Sum

1. 第一想法

3Sum 是我自己写写不出的状态,所以 4Sum 更是了。

2. 看完后想法

class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        nums.sort()
        n = len(nums)
        result = []
        for i in range(n):
            if nums[i] > target and nums[i] > 0 and target > 0:# 剪枝(可省)
                break
            if i > 0 and nums[i] == nums[i-1]:# 去重
                continue
            for j in range(i+1, n):
                if nums[i] + nums[j] > target and target > 0: #剪枝(可省)
                    break
                if j > i+1 and nums[j] == nums[j-1]: # 去重
                    continue
                left, right = j+1, n-1
                while left < right:
                    s = nums[i] + nums[j] + nums[left] + nums[right]
                    if s == target:
                        result.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
                    elif s < target:
                        left += 1
                    else:
                        right -= 1
        return result

3. 总结

常看常新。