【LeetCode】打卡--Python3算法15. 三数之和

版权声明:转载请说明出处,谢谢 https://blog.csdn.net/Asher117/article/details/89214464

【LeetCode】打卡–Python3算法15. 三数之和

题目

给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

注意:答案中不可以包含重复的三元组。

例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为:
[
  [-1, 0, 1],
  [-1, -1, 2]
]

结果

执行用时 : 1772 ms, 在3Sum的Python3提交中击败了36.63% 的用户
内存消耗 : 16.6 MB, 在3Sum的Python3提交中击败了71.41% 的用户

Python解答

这题真不容易,首先我使用两层循环,如下,结果显示超出时间限制:

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        data = []
        nums.sort()
        for i in range(len(nums)):
            if(nums[i]>0):
                break
            if(i>=1 and nums[i]==nums[i-1]):
                continue
            
            for j in range(i+1,len(nums)):
                if(-(nums[i] + nums[j]) in nums[j+1:]):
                    A = [nums[i],nums[j],-(nums[i] + nums[j])]
                    if(A not in data):
                        data.append(A)
        return data

然后尝试使用一层循环,加上双向指针,如下,结果还是超出时间限制:

class Solution:
    def threeSum(self, nums: [int]) -> [[int]]:
        data = []
        nums.sort()
        print(nums)
        for i in range(len(nums)-2):
            if(nums[i]>0):
                break
            if(i>=1 and nums[i]==nums[i-1]):
                continue
            m = i + 1
            n = len(nums) - 1
            while(m < n):
                total = nums[m] + nums[n] + nums[i]
                if(total==0 and [nums[i],nums[m],nums[n]] not in data):
                    data.append([nums[i],nums[m],nums[n]])
                    m = m + 1
                    n = n - 1
                elif(total > 0):
                    while(nums[n-1]==nums[n] and n-1 > m):
                        n = n - 1
                    n = n - 1
                else:
                    print(i,m,n)
                    while(nums[m+1]==nums[m] and m+1 < n):
                        m = m + 1
                    m = m + 1
        return data

最后,通过参考别人的发现,把列表,换成集合set就可以,结果,果然可以,很尴尬。

class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        data = set()
        nums.sort()
        for i in range(len(nums)-2):
            if(nums[i]>0):
                break
            if(i>=1 and nums[i]==nums[i-1]):
                continue
            m = i + 1
            n = len(nums) - 1
            while(m < n):
                total = nums[m] + nums[n] + nums[i]
                if(total==0):
                    data.add((nums[i],nums[m],nums[n]))
                    m = m + 1
                    n = n - 1
                elif(total > 0):
                    while(nums[n-1]==nums[n] and n-1 > m):
                        n = n - 1
                    n = n - 1
                else:
                    while(nums[m+1]==nums[m] and m+1 < n):
                        m = m + 1
                    m = m + 1
        return list(data)

我们下次再见,如果还有下次的话!!!
欢迎关注微信公众号:516数据工作室
516数据工作室

猜你喜欢

转载自blog.csdn.net/Asher117/article/details/89214464