【LeetCode】18. 四数之和

1 问题

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

  • 0 <= a, b, c, d < n
  • a、b、c 和 d 互不相同
  • nums[a] + nums[b] + nums[c] + nums[d] == 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
相关推荐
BB_CC_DD28 分钟前
四. 以Annoy算法建树的方式聚类清洗图像数据集,一次建树,无限次聚类搜索,提升聚类搜索效率。(附完整代码)
深度学习·算法·聚类
梁下轻语的秋缘2 小时前
每日c/c++题 备战蓝桥杯 ([洛谷 P1226] 快速幂求模题解)
c++·算法·蓝桥杯
CODE_RabbitV2 小时前
【深度强化学习 DRL 快速实践】逆向强化学习算法 (IRL)
算法
mit6.8242 小时前
[贪心_7] 最优除法 | 跳跃游戏 II | 加油站
数据结构·算法·leetcode
keep intensify3 小时前
通讯录完善版本(详细讲解+源码)
c语言·开发语言·数据结构·算法
shix .3 小时前
2025年PTA天梯赛正式赛 | 算法竞赛,题目详解
数据结构·算法
风铃儿~3 小时前
Java面试高频问题(26-28)
java·算法·面试
wuqingshun3141593 小时前
蓝桥杯 4. 卡片换位
算法·职场和发展·蓝桥杯
江沉晚呤时3 小时前
深入了解C# List集合及两种常见排序算法:插入排序与堆排序
windows·sql·算法·oracle·c#·排序算法·mybatis
Eric.Lee20213 小时前
数据集-目标检测系列- F35 战斗机 检测数据集 F35 plane >> DataBall
人工智能·算法·yolo·目标检测·计算机视觉