代码随想录day7

题目一

两个for循环,把四数之和转换为两数之和

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
        """
        table={}
        for i in nums1:
            for j in nums2:
                table[i+j]=table.get(i+j,0)+1
        
        count=0

        for i in nums3:
            for j in nums4:
                if -(i+j) in table:
                    count+=table[-(i+j)]
        return count

题目二、三

class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: 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:
                if nums[i]+nums[left]+nums[right]<0:
                    left+=1
                elif nums[i]+nums[left]+nums[right]>0:
                    right-=1
                else:
                    result.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 result
                    
class Solution(object):
    def fourSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        nums.sort()
        result=[]
        for i in range(len(nums)):
            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,len(nums)):    
                if nums[i]+nums[j]>target and nums[i]+nums[j]>0 and target>0:
                    break
                if j>i+1 and nums[j]==nums[j-1]:
                    continue
                    
                left=j+1
                right=len(nums)-1
                while left<right:
                    _sum=nums[i]+nums[j]+nums[left]+nums[right]
                    if _sum<target:
                        left+=1
                    elif _sum>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

                    

题目四

class Solution(object):
    def canConstruct(self, ransomNote, magazine):
        """
        :type ransomNote: str
        :type magazine: str
        :rtype: bool
        """
        table=[0]*26
        for i in magazine:
            table[ord(i)-ord('a')]+=1
        
        for j in ransomNote:
            table[ord(j)-ord('a')]-=1

        for n in table:
            if n<0:
                return False

        return True

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值