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 ...
随机推荐
- Ultra-QuickSort——[归并排序、分治求逆序对]
Description In this problem, you have to analyze a particular sorting algorithm. The algorithm proce ...
- 关于css中浮动的理解及实际应用
一.元素浮动的意义及使用:1. 浮动的意义:设置了浮动属性的元素会脱离普通标准流的控制,移动到其父元素中指定的位置的过程,将块级元素放在一行,浮动会脱离标准流,不占位置,会影响标准流,浮动只有左右浮动 ...
- vue-learning:3-template-{{}}-and-v-html
插值{{ }} 和 v-html 本节开始,我们按如下顺序学习vue模板API-指令.点击各部分的DEMO可在线查看代码 输出字符串文本内容的插值:{{}} 输出HMTL元素作为内容的指令:v-htm ...
- 使用Glide加载圆角图片
//设置图片圆角角度 RoundedCorners roundedCorners= new RoundedCorners(6); //通过RequestOptions扩展功能,override采样率, ...
- CS224n: Natural Language Processing学习准备
cs224n 斯坦福网址,里面包含讲课视频,ppt,代码,学习完后做一个问答系统 http://web.stanford.edu/class/cs224n/index.html 下载anaconda, ...
- Keras mlp 手写数字识别示例
#基于mnist数据集的手写数字识别 #构造了三层全连接层组成的多层感知机,最后一层为输出层 #基于Keras 2.1.1 Tensorflow 1.4.0 代码: import keras from ...
- vagrant在windows下的安装和配置(二)
在(一)中安装和配置好后 框框中的信息是登录vagrant up后的系统用的 我这里登录用的是xshell-----下载一个xshell然后安装 打开xshell 按确定之后生成一个新的会话,然后登录 ...
- Vijos 1206 CoVH之再破难关 [BFS] [位运算]
1.题意:一个由01组成的4*4的矩阵,可以实现相邻元素交换位置的操作,给出初试状态和目标状态,试求最少操作数的方案: 2.输入输出:输入给出初试矩阵和目标矩阵:要求输出最小操作的次数: 3.分析:输 ...
- Linux下搭建实现HttpRunnerManager的异步执行、定时任务及任务监控
前言 在之前搭建的HttpRunnerManager接口测试平台,我们还有一些功能没有实现,比如异步执行.定时任务.任务监控等,要完成异步执行,需要搭建 RabbitMQ 等环境,今天我们就来实现这些 ...
- jeecg中自定义按钮时遇到的问题