[LeetCode]题15:3Sum
第一次解:
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的更多相关文章
- LeetCode:15. 3Sum(Medium)
1. 原题链接 https://leetcode.com/problems/3sum/description/ 2. 题目要求 数组S = nums[n]包含n个整数,请问S中是否存在a,b,c三个整 ...
- 刷题15. 3Sum
一.题目说明 题目非常简洁15. 3Sum,读懂题目后,理解不难. 但 实话说,我们提交代码后,Time Limit Exceeded,最主要的是给了非常长的测试用例,我本地运行后87秒,确实时间非常 ...
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- 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 ...
- 《LeetBook》leetcode题解(15):3Sum[M]
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- 【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 ...
- 【一天一道LeetCode】#15 3Sum
一天一道LeetCode系列 (一)题目 Given an array S of n integers, are there elements a, b, c in S such that a + b ...
- Leetcode Array 15 3sum
思考的方向不对,即使用了多于别人几倍的时间,也不一定能够达到终点. 我的错误的想法(可以跳过):在leetcode上面做的第四道题,走路一个很大的弯路,收到之前做过的 Container With ...
- 【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 ...
随机推荐
- ng2-tree
[转]https://github.com/valor-software/ng2-tree#eyes-demo demo:http://valor-software.com/ng2-tree/
- 对text字段聚合,没有设置fielddate所以出错
http://192.168.60.26:9200/linewell_assets_mgt_es_yh_test/lw_devices/ _mapping { "properties&quo ...
- SVN修改已经提交过记录的日志和作者
原 SVN修改已经提交过记录的日志和作者 使用TortoiseSVN管理代码,对于已经提交的记录,可以修改提交作者和提交日志,不过会报如下错误: Repository has not been ena ...
- Windows 下运行Makefile文件
下载并安装Microsoft Visual Studio2017 配置环境变量: 计算机右击-属性-高级系统设置-环境变量-选择Path编辑-添加nmake的路径: D:\Microsoft Visu ...
- cocos2d-x JS 富文本
var str1 = "兑换成功后,系统会生成“";var str2 = "红包兑换码";var str3 = "”,请复制该兑换码,并粘贴在&quo ...
- 去掉vim的BELL提示音
title: date: 2017-11-09 15:07:08 tags: vim categories: 开发工具 --- 在vi/vim中使用 :set noeb 意思是noerrorbells ...
- 以选项卡的故事扯扯js面向对象
在现在的网页中,选项卡(我自己这样子叫)是非常普遍的,也是比较基础,学了原型实现选项卡也挺久了,最近在学ES6,学了用类实现选项卡,今天就在此做个总结,别的废话也不多说. 以"貌" ...
- jmeter获取mysql数据并作为请求参数使用
1.将mysql-connector-java-5.1.22-bin.jar包放到jmeter的lib目录下,重启jmeter 2.测试计划中添加jdbc connection,右键测试计划----添 ...
- 根据MAC地址获取网络地址及ZDP_NwkAddrReq函数的用法
1..对于设备需要获取本设备的网络地址和MAC地址: NLME_GetShortAddr()——返回本设备的16位网络地址 NLME_GetExtAddr()—— 返回本设备的64位扩展地址 2.使 ...
- MySQL插入更新_ON DUPLICATE KEY UPDATE
前提:操作的表具有主键或唯一索引 INSERT INTO:表中不存在对应的记录,则插入:若存在对应的记录,则报错: INSERT INTO IGNORE:表中不存在对应的记录,则插入:若存在对应的记录 ...