LeetCode 15 3Sum 解题思路和python代码

题目:

Given an integer array nums, return all the triplets nums\[i, numsj, numsk] such that i != j, i != k, and j != k, and numsi + numsj + numsk == 0.

Notice that the solution set must not contain duplicate triplets.

Example 1:

Input: nums = -1,0,1,2,-1,-4

Output: \[-1,-1,2,-1,0,1]

Explanation:

nums0 + nums1 + nums2 = (-1) + 0 + 1 = 0.

nums1 + nums2 + nums4 = 0 + 1 + (-1) = 0.

nums0 + nums3 + nums4 = (-1) + 2 + (-1) = 0.

The distinct triplets are -1,0,1 and -1,-1,2.

Notice that the order of the output and the order of the triplets does not matter.

Example 2:

Input: nums = 0,1,1

Output: \[\]

Explanation: The only possible triplet does not sum up to 0.

Example 3:

Input: nums = 0,0,0

Output: \[0,0,0]

Explanation: The only possible triplet sums up to 0.

Constraints:

3 <= nums.length <= 3000

-105 <= numsi <= 105

解题思路:

首先对数组进行排序,有助于使用双指针方法快速找到合适的三元组。

对于每个元素nums[i],将它作为第一个数,然后使用双指针方法在 i 之后的数组部分寻找另外两个数,使它们的和等于0。

左指针从 i+1 开始,右指针从数组末尾开始,逐步向中间收缩。根据三者之和的结果,调整左右指针的位置。

为了避免重复的三元组,遍历时需要跳过相同的元素。

python 复制代码
class Solution(object):
    def threeSum(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        result = []
        nums.sort()

        print("nums: ", nums)

        for i in range(len(nums)):
            print("i: ", i)

            # 如果当前元素和前一个相同,跳过,避免重复三元组
            if i > 0 and nums[i] == nums[i-1]:
                continue
            
            left, right = i+1, len(nums)-1

            while left < right:
                print("nums[i]: ", nums[i])
                print("nums[left]: ", nums[left])
                print("nums[right]: ", nums[right])
                total = nums[i] + nums[left] + nums[right]
                print("total: ", total)

                if total == 0:
                    result.append([nums[i], nums[left], nums[right]])

                    while left < right and nums[left] == nums[left+1]:
                        left+=1
                    while left < right and nums[right] == nums[right-1]:
                        right-=1
                    
                    left+=1
                    right-=1
                
                elif total < 0:
                    left += 1
                else:
                    right -= 1
        
        return result

例子:

nums = -1,0,1,2,-1,-4

首先,对数组进行排序。

('nums: ', -4, -1, -1, 0, 1, 2)

接着,遍历数组。

('i: ', 0)

('numsi: ', -4)

('numsleft: ', -1)

('numsright: ', 2)

('total: ', -3)

和小于0,说明左指针太小,向右移动左指针。

('numsi: ', -4)

('numsleft: ', -1)

('numsright: ', 2)

('total: ', -3)

和还是小于0,说明左指针太小,向右移动左指针。

('numsi: ', -4)

('numsleft: ', 0)

('numsright: ', 2)

('total: ', -2)

和还是小于0,说明左指针太小,向右移动左指针。

('numsi: ', -4)

('numsleft: ', 1)

('numsright: ', 2)

('total: ', -1)

和还是小于0,但 i=0 的遍历结束,进入 i=1。

('i: ', 1)

('numsi: ', -1)

('numsleft: ', -1)

('numsright: ', 2)

('total: ', 0)

和等于0,将三元组加入result。检查左指针指向的数(-1)是否与下一个数(0)相等,如果相等,就要跳过重复的左指针元素;同样道理,检查右指针指向的数(2)是否与前一个数(1)相等,如果相等,就跳过重复的右指针元素。

移动指针后,继续计算和。

('numsi: ', -1)

('numsleft: ', 0)

('numsright: ', 1)

('total: ', 0)

和等于0,将三元组加入result。

左指针大于右指针,遍历结束。

('i: ', 2)

nums2还是 -1,因此跳过。

('i: ', 3)

('numsi: ', 0)

('numsleft: ', 1)

('numsright: ', 2)

('total: ', 3)

左指针大于右指针,遍历结束。

('i: ', 4)

左指针等于右指针,while loop不会开始。

('i: ', 5)

左指针大于右指针,while loop不会开始。

output是 \[-1,-1,2,-1,0,1]

time complexity是O(n^2)

相关推荐
KaMeidebaby几秒前
卡梅德生物技术快报|纯化重组蛋白实操详解
人工智能·python·tcp/ip·算法·机器学习
Cloud_Shy6181 分钟前
解读《Effective Python 3rd Edition》:从练气到老魔(第五章 Item 30 - 32)
开发语言·人工智能·笔记·python·学习方法
天佑木枫28 分钟前
15天Python入门系列 · 序
开发语言·python
happylifetree28 分钟前
Python017-第二章15.数据容器-dict常用操作
python
装不满的克莱因瓶42 分钟前
了解 LangChain 中的 LLM 与 ChatModel 的差异
人工智能·python·ai·langchain·llm·agent·chatmodel
手写码匠1 小时前
从零实现 Prompt 工程引擎:结构化提示、自动优化与多轮自省体系
人工智能·深度学习·算法·aigc
无限码力1 小时前
阿里算法岗 0530笔试真题 - 多约束条件下的元素匹配统计
算法·阿里笔试真题·阿里机试真题·阿里算法岗笔试
lqqjuly1 小时前
MLA — 多头潜在注意力深度解析
深度学习·神经网络·算法
IT知识分享2 小时前
从零开发在线简繁转换工具:OpenCC 实战、避坑经验与方案选型
javascript·python
lunzi_08262 小时前
【学习笔记】《Python编程 从入门到实践》第8章:函数定义、参数传递与模块导入
笔记·python·学习