代码随想录算法训练营第7天|哈希表


题目链接:454.四数相加

思路:遍历前两个数组,将任意两个元素之和存入哈希表,key=两数之和,value=两数之和出现的次数;再遍历后两个数组,查看后两个数组元素之和的负数是否在哈希表中,若存在,count加哈希表中该数的次数

class Solution(object):
    def fourSumCount(self, nums1, nums2, nums3, nums4):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :type nums3: List[int]
        :type nums4: List[int]
        :rtype: int
        """
        unordered_map=collections.defaultdict(int)
        count=0
        for i in nums1:
            for j in nums2:
                unordered_map[i+j]+=1
        for i in nums3:
            for j in nums4:
                key=-i-j
                if key in unordered_map:
                    count+=unordered_map[key]
        return count


题目链接:383.赎金信

思路:和前面的字母异位词思路一致,定义一个数组遍历被组成字符串中字母出现的次数,再遍历另个提供字母的数组,若字母在数组中记录,则该记录减1;遍历结束后判断数组中是否存在大于0的记录,若存在,则说明提供字母的数组不够组成该字符串

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


题目链接:15.三数之和

思路:双指针法

结果添加之后,记得去重

class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        nums.sort()
        rs=[]
        
        for i in range(len(nums)):
            if nums[i]>0:
                return rs
            if i>0 and nums[i]==nums[i-1]:
                continue
            second=i+1
            right=len(nums)-1
            while second<right:
                sums=nums[i]+nums[second]+nums[right]
                if sums>0:
                    right-=1
                elif sums<0:
                    second+=1
                else:
                    rs.append([nums[i],nums[second],nums[right]])
                    while right>second and nums[right]==nums[right-1]:
                        right-=1
                    while second<right and nums[second]==nums[second+1]:
                        second+=1
                    right-=1
                    second+=1
        return rs


题目链接:18.四数之和

思路:和三数之和一样,利用双指针;只不过三数之和外层一层循环,四数之和外层两层循环

class Solution(object):
    def fourSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        rs=[]
        nums.sort()
        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:
                        right-=1
                    elif sums<target:
                        left+=1
                    else:
                        rs.append([nums[i],nums[j],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
        return rs

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值