第一次解:

        res = []
nums.sort()
if len(nums)<3:return []
for i in range(len(nums)-2):
left = i+1
right = len(nums)-1
while left < right:
val = nums[i]+nums[left]+nums[right]
if val==0 and [nums[i],nums[left],nums[right]] not in res:
res.append([nums[i],nums[left],nums[right]])
elif val<0:
left += 1
else:
right -=1
return res

超时。。。然后调整

nums.sort()
res = [] for i in range(len(nums) - 2):
if i > 0 and nums[i-1] == nums[i]: continue
l, r = i + 1, len(nums) - 1
while l < r:
s = nums[i] + nums[l] + nums[r]
if s == 0:
res.append([nums[i], nums[l], nums[r]])
l += 1; r -= 1
while l < r and nums[l] == nums[l-1]: l += 1
while l < r and nums[r] == nums[r+1] : r -= 1
elif s < 0:
l += 1
else:
r -= 1
return res

acc了

[LeetCode]题15:3Sum的更多相关文章

  1. LeetCode:15. 3Sum(Medium)

    1. 原题链接 https://leetcode.com/problems/3sum/description/ 2. 题目要求 数组S = nums[n]包含n个整数,请问S中是否存在a,b,c三个整 ...

  2. 刷题15. 3Sum

    一.题目说明 题目非常简洁15. 3Sum,读懂题目后,理解不难. 但 实话说,我们提交代码后,Time Limit Exceeded,最主要的是给了非常长的测试用例,我本地运行后87秒,确实时间非常 ...

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

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

  4. LeetCode OJ 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 ...

  5. 《LeetBook》leetcode题解(15):3Sum[M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  6. 【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 ...

  7. 【一天一道LeetCode】#15 3Sum

    一天一道LeetCode系列 (一)题目 Given an array S of n integers, are there elements a, b, c in S such that a + b ...

  8. Leetcode Array 15 3sum

      思考的方向不对,即使用了多于别人几倍的时间,也不一定能够达到终点. 我的错误的想法(可以跳过):在leetcode上面做的第四道题,走路一个很大的弯路,收到之前做过的 Container With ...

  9. 【LeetCode】15. 3Sum 三个数和为0

    题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

随机推荐

  1. python获取一年所有的日期

    python获取一年所有的日期 自动识别闰年. import arrow def isLeapYear(years): ''' 通过判断闰年,获取年份years下一年的总天数 :param years ...

  2. unity 安装破解提示partern not found和tutorials学习

    1.破解安装问题:unity 安装破解提示partern not found 可能和安装了多个版本有关,删除了c下的unity目录也没有解决,试着重新安装也不能破解, 破解的软件的方式是生成lisen ...

  3. node 解析图片二维码的内容

    const {readFile, readFileSync} = require('fs'); const decodeImage = require('jimp').read; const qrco ...

  4. IIS相关

    使用aspnet_regiis.exe重新注册.NET Framework 重新安装IIS以后,需要用aspnet_regiis.exe来注册.NET Framework, 如下: C:\WINDOW ...

  5. https学习笔记二----基础密码学知识和python pycrypto库的介绍使用

    在更详细的学习HTTPS之前,我也觉得很有必要学习下HTTPS经常用到的加密编码技术的背景知识.密码学是对报文进行编解码的机制和技巧.可以用来加密数据,比如数据加密常用的AES/ECB/PKCS5Pa ...

  6. 大堆文字不如几张图片-论信息传递的方式以NodeMCU入门为例

  7. extends 与implements的区别和用法

    1. 在类的声明中,通过关键字extends来创建一个类的子类.一个类通过关键字implements声明自己使用一个或者多个接口. extends 是继承某个类, 继承之后可以使用父类的方法, 也可以 ...

  8. Array类型和方法

    var ft = new Array(); var ft = new Array(20); var ft = new Array("1","2","3 ...

  9. flask重要点

    django与flask的区别 django: 大而全的框架,包含了很多组件,例如:ORM.form.ModelForm.session... flask: 轻量级的可扩展强的框架.有丰富的第三方组件 ...

  10. 雾霾天出行,如何精确避开“雷区”?2016 SODA数据侠十强

    (2016年参加了上海 SODA 竞赛,进入前十,最终获得上海市的两个奖项.) ▍跟踪雾霾,仅靠零星的监测点数据怎么行? 如果雾霾短期内没有办法彻底根治,我们可以做什么,把环境污染物对人的影响尽可能降 ...