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. 关于device tree中的interrupts选项

    http://blog.csdn.net/jiang__jiang/article/details/52064715 随着Linux的发展,dts技术是大势所趋.里面的interrupts = < ...

  2. 使用commons-net做FTP功能的异常 java.lang.ClassNotFoundException: org.apache.oro.text.regex.Malformed

    最近使用Apache的commons-net.jar做FTP上传下载功能,点击“上传”的时候报错,如下: java.lang.ClassNotFoundException: org.apache.or ...

  3. CORS跨域-Nginx使用方法(Access-Control-Allow-Origin错误提示)

    问题说明 当出现上图这个的时候,是访问请求外域URL无法访问,浏览器认为访问外域URL不安全,导致访问不了简称跨域问题.而这上面出现一句很重要的话“NO Access-Control-Allow-Or ...

  4. c#如何调用另外一个项目的类

    添加引用即可. 参考资料: https://zhidao.baidu.com/question/241402877.html http://blog.csdn.net/a1027/article/de ...

  5. Chap4:区块链的应用技术[《区块链中文词典》维京&甲子]

  6. [development][profile][dpdk] KK程序性能调优

    KK程序: 1. 两个线程,第一个从DPDK收包,通过一个ring数据传递给第二个线程.第二个线程将数据写入共享内存. 2. 第二个内存在发现共享内存已满时,会直接丢弃数据. 3. 线程二有个选项de ...

  7. Fmod使用总结

    1.查询相关文档的地址 http://www.fmod.org/forum/viewtopic.php?f=7&t=15762

  8. mysql设置指定ip访问,用户权限相关操作

    基础语法GRANT priv_type ON database.table TO user[IDENTIFIED BY [PASSWORD] 'password'] [,user [IDENTIFIE ...

  9. input的placeholder在ie9下不兼容的结局办法。

      /*      IE9placeholder支持      */     if(!placeholderSupport()){   // 判断浏览器是否支持 placeholder         ...

  10. Java如何连接SQLServer,并实现查询、修改、删除方法

    场景:A:在UI自动化时,删除数据时候,在界面UI提示“该XX已被使用,无法删除”. 这时候我们有需要做数据初始化的操作,需要把历史数据做删除,来确脚本运行的重复执行,和稳定性质. B: 在做新增操作 ...