代码随想录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
相关推荐
怪兽20141 小时前
fastjson在kotlin不使用kotlin-reflect库怎么使用?
android·开发语言·kotlin
ClearLiang1 小时前
Kotlin-协程的挂起与恢复
开发语言·kotlin
彭同学学习日志1 小时前
Kotlin Fragment 按钮跳转报错解决:Unresolved reference ‘floatingActionButton‘
android·开发语言·kotlin
海域云赵从友1 小时前
破解跨境数据传输瓶颈:中国德国高速跨境组网专线与本地化 IP 的协同策略
开发语言·php
咚咚王者1 小时前
人工智能之编程进阶 Python高级:第九章 爬虫类模块
开发语言·python
会编程的林俊杰1 小时前
SpringBoot项目启动时的依赖处理
java·spring boot·后端
一叶飘零_sweeeet2 小时前
深度拆解汽车制造系统设计:用 Java + 设计模式打造高扩展性品牌 - 车型动态生成架构
java·设计模式·工厂设计模式
深蓝海拓2 小时前
使matplot显示支持中文和负号
开发语言·python
王家羽翼-王羽2 小时前
nacos 3.1.0 运行主类报错 com.alibaba.cloud.nacos.logging.NacosLoggingAppRunListener
java
syt_biancheng3 小时前
Day3算法训练(简写单词,dd爱框框,3-除2!)
开发语言·c++·算法·贪心算法