【LeetCode每天一题】3Sum(三数之和)
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(三数之和)的更多相关文章
- leetcode第15题:三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- LeetCode:最接近的三数之和【16】
LeetCode:最接近的三数之和[16] 题目描述 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这 ...
- [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 ...
- [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 ...
- LeetCode(15):三数之和
Medium! 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答 ...
- Java实现 LeetCode 16 最接近的三数之和
16. 最接近的三数之和 给定一个包括 n 个整数的数组 nums 和 一个目标值 target.找出 nums 中的三个整数,使得它们的和与 target 最接近.返回这三个数的和.假定每组输入只存 ...
- [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 ...
- LeetCode每天一题之两数之和
这个LeetCode刷题系列的博客权当是为自己记一下笔记吧.博客系列会从LeetCode的第一题开始刷,同时会从零开始学习[因为我就是零/(ㄒoㄒ)/~~].同时,如果有写错的地方,希望大佬们在评论区 ...
- leecode第十五题(三数之和)
class Solution { public: void quick_order(vector<int>& num, int star, int en)//快排 { int st ...
随机推荐
- springboot JPA
JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据.他的出现主要是为了简化现有的持久 ...
- python3之深浅拷贝
深浅copy 1,先看赋值运算. l1 = [1,2,3,['barry','alex']] l2 = l1 l1[0] = 111 print(l1) # [111, 2, 3, ['barry', ...
- 在服务器上搭建git仓库
文档 管理全部用户的公匙 /tmp/id_rsa.ajanuw.pub // 这里全部放在 /tmp目录下 在服务器上创建一个名叫 git 的用户 adduser git // 一路回车 passwd ...
- ssh文件配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- iPhone XS 能否经受的起寒冬的考验
我的知乎文章链接: https://zhuanlan.zhihu.com/p/51782644 华北地区近日寒风凛冽,温度骤降,已经进入真正的冬天了,最低温度可以达到零下10度,我们手里的iPhone ...
- atoi函数的用法
库函数原型: #inclue <stdlib.h> int atoi(const char *nptr); 用法:将字符串里的数字字符转化为整形数.返回整形值. 注意:转化时跳过前面的空格 ...
- BZOJ 1003 - 物流运输 - [最短路+dp]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1003 Time Limit: 10 Sec Memory Limit: 162 MB D ...
- Exception 06 : org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session :
异常名称: org.hibernate.NonUniqueObjectException: A different object with the same identifier value was ...
- java mvc spring boot
spring mvchttp://www.cnblogs.com/wcf6676/p/5333352.html 利用mybatis-generator自动生成代码http://www.cnblogs. ...
- hdfs mapreduce hbase
参考资料:http://www.cnblogs.com/sharpxiajun/p/5585613.html 大数据时代的数据量是超大规模的,传统的关系数据库已经很难存储和管理这些数据了,为了存储海量 ...