1. 题目

2.题目分析与思路

3.思路

1. 题目

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

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

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

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

2. 思路

  这道题最直接想到的应该是两数之和,两数之和还是比较基础的,采用通知记录的方式,维护一个字典,看新的数是否属于这个字典的键即可。三数之和也可以使用类似的办法,但是题目要求的是不能有重复的,这就比较难办了,那可能只有先将其排序,然后判断一下他们是否在集合中,思路就显而意见了,代码如下:

def threeSum(self, nums: List[int]) -> List[List[int]]:
result = []
for i,j in enumerate(nums):
temp = nums[:i]+nums[i+1:]
dic1 = {}
dic2 = {}
for count,k in enumerate(temp):
if k not in dic1:
dic1[-j-k] = k
else:
dic2[tuple(sorted([j,k,-j-k]))] = [j,k,-j-k]
return dic2.values()

3. 改进

然而不幸的是,这个复杂度太高,跑不过所有case便会超时,在这之前我使用的是判断list是否在list中,这样的话更没有办法通过所有的case,复杂度太高,优化以后使用字典但还是在全是0的case失败了。

经过修改后和一些边界条件,给出一个通过了case,但是极其慢的解法,我称其为无情解法:

def threeSum(self, nums: List[int]) -> List[List[int]]:
dic2 = {}
if (len(set(nums) ) == 1)and (len(nums) > 2): #主要是去除全是0 的情况,全是0 就会导致最后的循环复杂度过高
if 0 in set(nums):
return [[0,0,0]]
for i,j in enumerate(nums):
temp = nums[:i]+nums[i+1:]
dic1 = {}
for count,k in enumerate(temp):
if k not in dic1:
dic1[-j-k] = k
else:
dic2[tuple(sorted([j,k,-j-k]))] = [j,k,-j-k]
return dic2.values()

下面给出正确的解法,使用双指针

class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums = sorted(nums)
res = []
dic1 = {}
for i,j in enumerate(nums):
if j > 0:
continue
temp = nums[i+1:]
left = 0
right = len(temp)-1
while(left < right): if j + temp[left]+temp[right] == 0:
dic1[(j,temp[left],temp[right])] = [j,temp[left],temp[right]]
# res.append([j,temp[left],temp[right]])
right -= 1
elif j + temp[left]+temp[right] > 0:
right -= 1
else:
left += 1
return dic1.values()

LeetCode 第15题-三数之和的更多相关文章

  1. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

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

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

  3. LeeCode数组第15题三数之和

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

  4. leetcode 刷题(数组篇)15题 三数之和 (双指针)

    很有意思的一道题,值得好好思考,虽然难度只有Mid,但是个人觉得不比Hard简单 题目描述 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b ...

  5. Leetcode(15)-三数之和

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

  6. [LeetCode] 259. 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  7. 【JavaScript】Leetcode每日一题-平方数之和

    [JavaScript]Leetcode每日一题-平方数之和 [题目描述] 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c . 示例1: 输入:c = 5 ...

  8. [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 ...

  9. [LeetCode] 3Sum Closest 最近三数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

随机推荐

  1. UVa1601 - The Morning after Halloween [单向bfs]

    解题思路: 1.注意到2*2方格中必有一个#,那么最多只有192条通道,可以将所有非‘#’的位置提取出来用邻接表的方式建图,通过bfs搜索目标位置. 2.将三个ghost的位置(a,b,c)作为状态量 ...

  2. linux 不用 ioctl 的设备控制

    有时控制设备最好是通过写控制序列到设备自身来实现. 例如, 这个技术用在控制台驱动 中, 这里所谓的 escape 序列被用来移动光标, 改变缺省的颜色, 或者进行其他的配置任 务. 这样实现设备控制 ...

  3. Executor线程池的最佳线程数量计算

    如果是IO密集型应用,则线程池大小设置为2N+1: 如果是CPU密集型应用,则线程池大小设置为N+1: N代表CPU的核数. 假设我的服务器是4核的,且一般进行大数据运算,cpu消耗较大,那么线程池数 ...

  4. get_free_page 和其友

    如果一个模块需要分配大块的内存, 它常常最好是使用一个面向页的技术. 请求整个页也 有其他的优点, 这个在 15 章介绍. 为分配页, 下列函数可用: get_zeroed_page(unsigned ...

  5. 【15.93%】【codeforces 672D】Robin Hood

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. Python 多组输入

    #基于Python2.7 #若是想Python做到和C++中while(scanf()!=EOF)一样的多组输入效果,可以如实例所示书写 #实例实现了多组输入,计算A+B+C并输出的任务 while ...

  7. c#链接redis用户名密码

    方法一 使用:ServiceStack.Redis 在ip:port前面加上@用来表示密码,比如password@ip:port <add key="RedisServer" ...

  8. sql函数实用——字符函数(sqlserver与mysql对比)

    1.获取长度 sqlserver写法:关键字:len()    获取参数的字符数量 select  Len('aksjdhh')    输出结果 7 select len('张无忌ooo')   输出 ...

  9. 「Luogu P3931」SAC E#1 - 一道难题 Tree 解题报告

    圆原题面 我环顾四周,发现大佬们的写法都好高端! 比较差劲的我,只能交上一份DFS的题解 思路: DFS(当然了,其他算法也行) 要想切断叶子节点到根节点的连接 就是在叶子节点和根节点之间砍掉一条边 ...

  10. linux下配置vnc-server 和gnome-session

    机器比较老,安装时间也十分久远,所以也不知道实验室系统当时是不是完全安装,最近需要使用vnc登录显示界面,结果问题就来了...没有安装vnc-server. (1)机器系统是rhel6.2的,所以就从 ...