LeetCode 第15题-三数之和
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题-三数之和的更多相关文章
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- LeeCode数组第15题三数之和
题目:三数之和 内容: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中 ...
- leetcode 刷题(数组篇)15题 三数之和 (双指针)
很有意思的一道题,值得好好思考,虽然难度只有Mid,但是个人觉得不比Hard简单 题目描述 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b ...
- Leetcode(15)-三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- [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 < ...
- 【JavaScript】Leetcode每日一题-平方数之和
[JavaScript]Leetcode每日一题-平方数之和 [题目描述] 给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c . 示例1: 输入:c = 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 ...
- [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 ...
随机推荐
- linux readv 和 writev
Unix 系统已经长时间支持名为 readv 和 writev 的 2 个系统调用. 这些 read 和 write 的"矢量"版本使用一个结构数组, 每个包含一个缓存的指针和一个 ...
- Java中的断言assert的用法
Java陷阱之assert关键字 一.概述 在C和C++语言中都有assert关键,表示断言. 在Java中,同样也有assert关键字,表示断言,用法和含义都差不多. 二.语法 在Java中,ass ...
- 2018.12.7 浪在ACM 集训队第八次测试赛
2018.12.7 浪在ACM 集训队第八次测试赛 https://blog.csdn.net/QLU_minoz/article/details/84886717 感谢苗学林同学C题和D题题解 ...
- CodeForces - 1162E Thanos Nim (博弈论)
Alice and Bob are playing a game with nn piles of stones. It is guaranteed that nn is an even number ...
- 2019-9-2-win10-uwp-获得焦点改变
title author date CreateTime categories win10 uwp 获得焦点改变 lindexi 2019-09-02 12:57:38 +0800 2018-2-13 ...
- SAPI(PHP常见的四种运行模式)
SAPI(Server Application Programming Interface)服务器应用程序编程接口,即PHP与其他应用交互的接口,PHP脚本要执行有很多方式,通过Web服务器,或者直接 ...
- 【软件】Gimp内容识别填充开启工具1.0
*源文件起初存于Gimp-Registry,后因服务器维护成本问题,Gimp将原服务器中所有文件迁移至GimpGithub *博主只是将原插件中的内容识别填充功能进行打包处理,方便大家一键启用. 下载 ...
- lombok优缺点
优点: 能通过注解的形式自动生成构造器.getter/setter.equals.hashcode.toString等方法,提高了一定的开发效率 让代码变得简洁,不用过多的去关注相应的方法 属性做修改 ...
- 【重学Node.js 第1&2篇】本地搭建Node环境并起RESTful Api服务
本地搭建Node环境并起RESTful Api服务 课程介绍看这里:https://www.cnblogs.com/zhangran/p/11963616.html 项目github地址:https: ...
- 《美国纽约摄影学院摄影教材》PDF教材
下载地址: 美国纽约摄影学院摄影教材(上册).pdf 美国纽约摄影学院摄影教材(下册).pdf 欢迎你到纽约摄影学院来,我们急切地等待着开课,你们也在 跃跃欲试了.那就让我们马上开始吧! 你已 ...