代码随想录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
相关推荐
Tony Bai几秒前
【Go 网络编程全解】12 本地高速公路:Unix 域套接字与网络设备信息
开发语言·网络·后端·golang·unix
oioihoii10 分钟前
深入理解 C++ 现代类型推导:从 auto 到 decltype 与完美转发
java·开发语言·c++
报错小能手14 分钟前
项目——基于C/S架构的预约系统平台 (1)
开发语言·c++·笔记·学习·架构
韩立学长27 分钟前
【开题答辩实录分享】以《租房小程序的设计和实现》为例进行答辩实录分享
java·spring boot·小程序
zl97989931 分钟前
SpringBoot-数据访问之MyBatis与Redis
java·spring boot·spring
MYX_30935 分钟前
第四章 多层感知机
开发语言·python
K_i13439 分钟前
Tomcat核心原理与运维实战指南
java·运维·tomcat
彬彬醤1 小时前
如何正确选择住宅IP?解析适配跨境、流媒体的网络工具
服务器·开发语言·网络·网络协议·tcp/ip
重生之我是Java开发战士1 小时前
【Java EE】快速上手Spring Boot
java·spring boot·java-ee
從南走到北1 小时前
JAVA国际版一对一视频交友视频聊天系统源码支持H5 + APP
java·微信·微信小程序·小程序·音视频·交友