Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: The solution set must not contain duplicate triplets.

Example: Given array nums = [-1, 0, 1, 2, -1, -4],              A solution set is:      [         [-1, 0, 1],           [-1, -1, 2]   ]

思路


  我们先对数组进行排序,然后从第一个数字开始查找, 然后在后面的数组中查找是否有满足条件的(使用两个指针一个指向开头,一个指向尾部开始遍历)。时间复杂度为O(n*n), 空间复杂度为O(1)。

图示步骤

解决代码


 class Solution(object):
def threeSum(self, nums):
nums.sort()
length = len(nums)
res_list = []
for i in range(length - ): # 以此下标的数字和其他进行匹配
if i > and nums[i] == nums[i - ]: # 如果和上一个数字相等,直接返回。 因为上一次已经查找过
continue
start, end = i + , length-1 # 在后面的数组中设置起始指针和尾部指针
while start < end:
tem = nums[i] + nums[start] + nums[end]
if tem == : # 如果三个数字之和等于0,将其进行添加
res_list.append([nums[i], nums[start], nums[end]]) while start < end and nums[start] == nums[start+]: # 判断start下一个数字和当前数字是否相等,相等下移一位。不然会添加进重复的元素
start += 1
while start < end and nums[end] == nums[end-]: # 判断end上一个数字和当前数字是否相等,相等则跳过。
end -=
start +=
end -=
elif tem < : # 和小于 0 的话,start进位
start +=
else: # 和大于0的话,end减一
end -=
return res_list

【LeetCode每天一题】3Sum(三数之和)的更多相关文章

  1. leetcode第15题:三数之和

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

  2. 【LeetCode】15. 3Sum 三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...

  3. LeetCode:最接近的三数之和【16】

    LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...

  4. [LeetCode] 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  5. [LeetCode] 15. 3Sum 三数之和

    Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...

  6. LeetCode(15):三数之和

    Medium! 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答 ...

  7. Java实现 LeetCode 16 最接近的三数之和

    16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...

  8. [leetcode]15. 3Sum三数之和

    Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find ...

  9. LeetCode每天一题之两数之和

    这个LeetCode刷题系列的博客权当是为自己记一下笔记吧.博客系列会从LeetCode的第一题开始刷,同时会从零开始学习[因为我就是零/(ㄒoㄒ)/~~].同时,如果有写错的地方,希望大佬们在评论区 ...

  10. leecode第十五题(三数之和)

    class Solution { public: void quick_order(vector<int>& num, int star, int en)//快排 { int st ...

随机推荐

  1. java注解和反射学习

    spring框架很多地方都应用了注解,如@controller,所以要学会自定义注解及注解处理器. Class<?> cl=Class.froName(className) //通过类名加 ...

  2. 10.8Xadmin url注册

    2018-10-8 16:45:31 这两天在弄我的服务器,就是是看各种教程 死活部署不好我的Django项目 博客网站 过几天再弄! 越努力,越幸运!永远不要高估自己! 通过两个方法,通过类变量找到 ...

  3. 比例尺与DEM空间分辨率

    我国 4 种比例尺 DEM 及其空间分辨率的对应关系:  1:100 万 DEM  --  1000m 空间分辨率:  1:25 万 DEM  --  100m 空间分辨率:  1:5 万 DEM  ...

  4. JavaScript中的Array类型详解

    与其他语言中的数组的区别: 1.JavaScript数组的每一项都可以保存任何类型的数据,一个数组的数组元素可以是不同类型的数据. 2.数组的大小是动态调整的,可以随着数据的添加自动的增长. 1.两种 ...

  5. TensorRT 进行推理

  6. iOS循环引用常见场景和解决办法

    好多场景会导致循环引用,例如使用Block.线程.委托.通知.观察者都可能会导致循环引用. 1.委托 遵守一个规则,委托方持有代理方的强引用,代理方持有委托方的弱引用. 实际场景中,委托方会是一个控制 ...

  7. en-zh(科学技术)science and technology

    S Korea to roll out 5G韩国正式推5G商用服务 South Korea will become the first country to commercially launch f ...

  8. C 缓冲区过读 if (index >= 0 && index < len)

    C 缓冲区过读 if (index >= 0 && index < len) CWE - CWE-126: Buffer Over-read (3.2) http://cw ...

  9. java发送get,post请求

    方法里面有注释:参照csdn里面的,项目用时自己改 package com.bst.express; import java.io.BufferedReader; import java.io.Dat ...

  10. 转:string.Format格式化用法

    String.format()方法使用说明(比较详细) 地址:https://blog.csdn.net/thc1987/article/details/17528093 典型案例 原文地址:http ...