【leetcode刷题】三数之和

本文介绍了一种改进的算法,用于解决三数之和问题,通过先排序和分类正负数,利用二分查找减少复杂度至O(n log n),适用于处理大型数据集。不再受限于原始O(n^2)的时间复杂度。

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

思路:先排序,取出大于等于0,小于0两个数组,在两个数组里各自取出一个值求和,在剩下的数字中找到与之互补的数字。
1、排序
2、将数据分为两个列表,一个为负数,一个为正数和0
3、在剩下的数中找和这两个数相加为0 的数
算法复杂度O(n^2*logn)
在这里插入图片描述

from typing import List
class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        #二分查找法
        def dichotomyFind(L, target):
            length = len(L)
            high = length - 1
            low = 0
            while (high >= low):
                if (L[int((high + low) / 2)] > target):
                    high = int((high + low) / 2) - 1
                elif L[int((high + low) / 2)] < target:
                    low = int((high + low) / 2) + 1
                else:
                    out = L[int((high + low) / 2)]
                    return out
            return 100001
        #正负数分类 并排序
        result = []
        zero = nums.count(0)
        if(zero>=3):        #得到三个都为0的答案
            result.append([0,0,0])


        Snums = nums.copy()
        Snums.sort()    #排序
        length = len(nums)
        count = 0
        for i in Snums:
            if i < 0:
                count += 1
            if i >= 0:
                break;
        small = Snums[0:count]
        big = Snums[count:length]


        for i in range(0, len(small)):      #从负数集合寻找点
            for j in range(len(big)-1, -1,-1):      #把最大的正数放进去
                target = -small[i] - big[j]
                if small[i] + big[j] <= 0:
                    res = dichotomyFind(big[0:j], target)
                    #print(res)
                else:
                    res = dichotomyFind(small[i+1:len(small)],target)
                    #print(res)
                if res != 100001:
                    re = [small[i], big[j], res]
                    re.sort()
                    if (re in result):
                        continue
                    else:
                        result.append(re)
                while j-1>-1:
                    if(big[j-1] == big[j]):
                        j = j - 1
                    else:
                        break
            while i + 1 < len(small):
                if (small[i+1] == small[i]):
                    i=i+1
                else:
                    break
        return result


nums = [1]
# #nums = [-1,0,1,2,-1,-4]
out = Solution().threeSum(nums)
print(out)

这个方法在解答的时候,是超出时间的。时间复杂度O(n^2logn)
在这里插入图片描述

from typing import List
class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        if(len(nums)<3):
            return []
        nums.sort()      #对列表进行排序
        result = []
        for i in range(0,len(nums)):
            L = i+1
            R = len(nums)-1
            while(L<R):
                if(nums[i]+nums[L]+nums[R]<0):
                    L+=1
                elif(nums[i]+nums[L]+nums[R]>0):
                    R-=1
                else:
                    re = [nums[i],nums[L],nums[R]]
                    L += 1
                    R -= 1
                    re.sort()
                    if(re not in result):
                        result.append(re)
        return result

时间复杂度O(n^2)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值