【LeetCode】18. 四数之和

1 问题

给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 nums\[a, numsb, numsc, numsd] (若两个四元组元素一一对应,则认为两个四元组重复):

  • 0 <= a, b, c, d < n
  • a、b、c 和 d 互不相同
  • numsa + numsb + numsc + numsd == target

你可以按 任意顺序 返回答案 。

示例 1:

输入:nums = 1,0,-1,0,-2,2, target = 0

输出:\[-2,-1,1,2,-2,0,0,2,-1,0,0,1]

示例 2:

输入:nums = 2,2,2,2,2, target = 8

输出:\[2,2,2,2]

2 答案

自己写的,感觉没问题

python 复制代码
class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        n = len(nums)
        res = []
        nums.sort()
        for i in range(n):
            if i>0 and nums[i]==nums[i-1]:
                continue
            for j in range(i+1, n):

                if j>1 and nums[j]==nums[j-1]:
                    continue
                L = j+1
                R = n-1
                while L<R:
                    if nums[i]+nums[j]+nums[L]+nums[R]==target:
                        res.append([nums[i], nums[j], nums[L], nums[R]])
                    if L<R and nums[L] == nums[L+1]:
                        L+=1
                    if L<R and nums[R] == nums[R-1]:
                        R-=1
                    
                    if nums[i]+nums[j]+nums[L]+nums[R] > target:
                        R-=1
                    else:
                        L+=1
        return res

总是写错一些地方,修改后可以运行

python 复制代码
class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        n = len(nums)
        res = []
        nums.sort()
        for i in range(n):
            if i>0 and nums[i]==nums[i-1]:
                continue
            for j in range(i+1, n):

                if j-i>1 and nums[j]==nums[j-1]:
                    continue
                L = j+1
                R = n-1
                while L<R:
                    if nums[i]+nums[j]+nums[L]+nums[R]==target:
                        res.append([nums[i], nums[j], nums[L], nums[R]])
                        while L<R and nums[L] == nums[L+1]:
                            L+=1
                        while L<R and nums[R] == nums[R-1]:
                            R-=1
                        L+=1
                        R-=1
                    elif nums[i]+nums[j]+nums[L]+nums[R] > target:  # 必须elif
                        R-=1
                    else:
                        L+=1
        return res

官方解,加了许多优化和剪枝的方法

python 复制代码
class Solution:
    def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
        n=len(nums)
        if(not nums or n<4):
            return []
        nums.sort()
        res=[]
        for i in range(n-3):
            if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target):  # 第一次遍历剪枝
                break
            if(nums[i]+nums[-1]+nums[-2]+nums[-3]<target):  # 第一次遍历剪枝,nums[-1]这里-1是指n-1
                continue
            if(i>0 and nums[i]==nums[i-1]):
                continue
            for j in range(i+1,n-2):
                if(nums[i]+nums[j]+nums[j+1]+nums[j+2]>target): # 第二次遍历剪枝
                    break
                if(nums[i]+nums[j]+nums[-1]+nums[-2]<target):  # 第二次遍历剪枝
                    continue
                if(j-i>1 and nums[j]==nums[j-1]):
                    continue
                L=j+1
                R=n-1
                while(L<R):
                    if(nums[i]+nums[j]+nums[L]+nums[R]==target):
                        res.append([nums[i],nums[j],nums[L],nums[R]])
                        while(L<R and nums[L]==nums[L+1]):
                            L=L+1
                        while(L<R and nums[R]==nums[R-1]):
                            R=R-1
                        L=L+1
                        R=R-1
                    elif(nums[i]+nums[j]+nums[L]+nums[R]>target):
                        R=R-1
                    else:
                        L=L+1
        return res
相关推荐
文心快码BaiduComate14 分钟前
2026 OPC x AI Coding 趋势案例交流会圆满落幕:一个人就一支研发团队
算法
ACM-moran28 分钟前
条件显化(二分单峰函数+构造数列)
算法·推公式·整数三分
闻缺陷则喜何志丹42 分钟前
【栈 运算系统】P3719 [AHOI2017初中组] rexp|普及+
c++·算法··洛谷·运算系统
疯狂打码的少年1 小时前
【数据库技术】多值依赖与第四范式(4NF)
数据库·笔记·算法
子文雨1 小时前
基于 uC3845 与 ATtiny13 双芯片架构的 8.4V/2.5Ah 锂电池智能充电器设计
单片机·嵌入式硬件·算法
青山木1 小时前
Hot 100 --- 数组中的第K个最大元素
java·数据结构·算法·排序算法
玛卡巴卡ldf1 小时前
【AICoding】笔试提示词设计思路
java·算法·ai编程
明月_清风2 小时前
位图与布隆过滤器:海量数据下的"存在性判断"艺术
前端·后端·算法
Rnan-prince2 小时前
堆与TopK · 从零到通透 —— 9 节全系列复盘
python·算法
明月_清风2 小时前
Hash 表从入门到精通:Go 实战与工程细节
前端·后端·算法