代码随想录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
相关推荐
Sam-August3 小时前
【分布式架构实战】Spring Cloud 与 Dubbo 深度对比:从架构到实战,谁才是微服务的王者?
java·spring cloud·dubbo
麦兜*3 小时前
MongoDB 常见错误解决方案:从连接失败到主从同步问题
java·数据库·spring boot·redis·mongodb·容器
ytadpole4 小时前
揭秘设计模式:命令模式-告别混乱,打造优雅可扩展的代码
java·设计模式
sinat_602035364 小时前
模块与包的导入
运维·服务器·开发语言·python
恋雨QAQ4 小时前
python函数和面向对象
开发语言·python
用户3721574261354 小时前
Java 教程:轻松实现 Excel 与 CSV 互转 (含批量转换)
java
天雪浪子4 小时前
Python入门教程之逻辑运算符
开发语言·python
叫我阿柒啊4 小时前
Java全栈开发实战:从基础到微服务的深度解析
java·微服务·kafka·vue3·springboot·jwt·前端开发
落羽的落羽4 小时前
【C++】特别的程序错误处理方式——异常机制
开发语言·c++