07天【代码随想录算法训练营34期】 第三章 哈希表part02(● 454.四数相加II ● 383. 赎金信 ● 15. 三数之和 ● 18. 四数之和)

454.四数相加II

class Solution:
    def fourSumCount(self, nums1: List[int], nums2: List[int], nums3: List[int], nums4: List[int]) -> int:
        table = dict()
        for i in nums1:
            for j in nums2:
                if (i + j) in table:
                    table[i+j] += 1
                else:
                    table[i+j] = 1
        
        result = 0
        for i in nums3:
            for j in nums4:
                if (-i-j) in table:
                    result += table[-i-j]
        return result

383. 赎金信 Ransom Note
Hash Table again

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

15. 三数之和
好难啊,有机会再做一遍吧

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        nums.sort()
        result = []

        for i in range(len(nums)):
            if nums[i] > 0:
                return result

            # avoid repitition
            if i > 0 and nums[i] == nums[i-1]:
                continue
            
            left = i + 1
            right = len(nums) - 1

            while left < right:
                sums = nums[i] + nums[left] + nums[right]
                if sums == 0:
                    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
                elif sums < 0:
                    left += 1
                else:
                    right -= 1
        return result

18. 四数之和
left和right移动的方向很关键,不要马虎哦

class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        nums.sort()
        result = []
        n = len(nums)

        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 = j+1
                right = n-1
                
                while left < right:
                    sums = nums[i] + nums[j] + nums[left] + nums[right]
                    
                    if sums < target:
                        left += 1
                    elif sums > target:
                        right -= 1
                    else:
                        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
        return result
posted @ 2024-03-26 15:46  MiraMira  阅读(13)  评论(0)    收藏  举报