【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
相关推荐
KarrySmile34 分钟前
Day04–链表–24. 两两交换链表中的节点,19. 删除链表的倒数第 N 个结点,面试题 02.07. 链表相交,142. 环形链表 II
算法·链表·面试·双指针法·虚拟头结点·环形链表
花开富贵ii40 分钟前
代码随想录算法训练营二十八天|动态规划part01
java·数据结构·算法·leetcode·动态规划
啊阿狸不会拉杆40 分钟前
《Java 程序设计》第 7 章 - 继承与多态
java·开发语言·jvm·算法·intellij-idea
技术卷1 小时前
详解力扣高频SQL50题之1164. 指定日期的产品价格【中等】
sql·leetcode·oracle
Deng9452013142 小时前
数独求解器与生成器(回溯算法实现)
算法·图形用户界面·matlab技术·数独谜题·求解器与生成器
淦暴尼2 小时前
银行客户流失预测分析
python·深度学习·算法
Swiler2 小时前
数据结构第1问:什么是数据结构?
数据结构·算法
Eloudy2 小时前
复矩阵与共轭转置矩阵乘积及其平方根矩阵
人工智能·算法·矩阵
m0_631354452 小时前
VTK开发day2:切片矩阵
人工智能·算法·矩阵
go54631584652 小时前
在本地环境中运行 ‘dom-distiller‘ GitHub 库的完整指南
人工智能·深度学习·神经网络·算法·矩阵·github