【leetcode】18. 四数之和

这篇博客介绍了如何使用排序和双指针优化算法解决LeetCode上的18题——四数之和问题。作者通过两种不同版本的代码展示了排序+双指针的方法,并分析了优化前后的时间复杂度,强调了在循环内添加条件判断来减少循环次数的重要性。优化后的算法运行时间从840ms降低到44ms。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

排序+双指针(优化前)

class Solution(object):
    def fourSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        res = list()
        length = len(nums)
        if length < 4:
            return res
        nums.sort()
        for i in range(length):
            if i > 0 and nums[i] == nums[i - 1]:
                continue
            j = i + 1
            while j < length:
                while i + 1 < j < length and nums[j] == nums[j - 1]:
                    j += 1
                L = j + 1
                R = length - 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:
                        L += 1
                    else:
                        R -= 1
                j += 1
        return res

排序+双指针(优化后)

class Solution(object):
    def fourSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[List[int]]
        """
        res = list()
        length = len(nums)
        if length < 4:
            return res
        nums.sort()
        for i in range(length - 3):
            if i > 0 and nums[i] == nums[i - 1]:
                continue
            if nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target:
                break
            if nums[i] + nums[length - 3] + nums[length - 2] + nums[length - 1] < target:
                continue
            for j in range(i + 1, length - 2):
                if j > i + 1 and nums[j] == nums[j - 1]:
                    continue
                if nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target:
                    break
                if nums[i] + nums[j] + nums[length - 2] + nums[length - 1] < target:
                    continue
                L = j + 1
                R = length - 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:
                        L += 1
                    else:
                        R -= 1
        return res

总结

1、该题目为15、16题的结合,只需在“三数之和”外层再添加一层循环即可。
但是,我们发现该方法的运行时间很长,时长达到“840ms”,需要优化。
2、在循环体内,添加判断语句:

if nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target:
    break
if nums[i] + nums[length - 3] + nums[length - 2] + nums[length - 1] < target:
    continue

if nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target:
    break
if nums[i] + nums[j] + nums[length - 2] + nums[length - 1] < target:
    continue

以此减少循环的次数。经过优化,时长减少至“44ms”。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值