代码随想录70期day7

454

python 复制代码
class Solution(object):
    def fourSumCount(self,nums1,nums2,nums3,nums4):
        hashmap = dict()
        for n1 in nums1:
            for n2 in nums2:
                hashmap[n1+n2] = hashmap.get(n1 + n2,0) + 1

        count = 0
        for n3 in nums3:
            for n4 in nums4:
                key = -n3 - n4 
                if key in hashmap:
                    count += hashmap[key]
        return count     

383

python 复制代码
from collections import defaultdict

class Solution:
    def canConstruct(self, ransomNote: str, magazine: str) -> bool:

        hashmap = defaultdict(int)

        for x in magazine:
            hashmap[x] += 1

        for x in ransomNote:
            value = hashmap.get(x)
            if not value:
                return False
            else:
                hashmap[x] -= 1

        return True

15

python 复制代码
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

18

python 复制代码
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
相关推荐
程序员小假12 分钟前
线程池执行过程中遇到异常该怎么办?
java·后端
稚辉君.MCA_P8_Java16 分钟前
DeepSeek Java 单例模式详解
java·spring boot·微服务·单例模式·kubernetes
洛_尘24 分钟前
数据结构--4:栈和队列
java·数据结构·算法
疯癫的老码农30 分钟前
【小白入门docker】创建Spring Boot Hello World应用制作Docker镜像并运行
java·spring boot·分布式·docker·微服务
Mr.Entropy39 分钟前
Hibernate批量查询方法全面解析
java·后端·hibernate
绝顶少年1 小时前
Spring 框架中 RestTemplate 的使用方法
java·后端·spring
小趴菜82271 小时前
安卓人机验证View
android·java·前端
信安成长日记1 小时前
golang 写路由的时候要注意
开发语言·后端·golang
那个什么黑龙江1 小时前
关于C++中的“类中的特殊成员函数”
开发语言·c++
观望过往1 小时前
【Java SE 运算符】全面解析与实践指南
java