[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 ...
随机推荐
- el表达式(一)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- Spring Boot事务管理(中)
在上一篇 Spring Boot事务管理(上)的基础上介绍Spring Boot事务属性和事务回滚规则 . 4 Spring Boot事务属性 什么是事务属性呢?事务属性可以理解成事务的一些基本配置, ...
- Web 开发技术文档大全
https://developer.mozilla.org/zh-CN/docs/Web 基础 HTML CSS HTTP 脚本 JavaScript Web API 事件 Web Component ...
- vue-cli 搭建的项目关闭 eslint
一般不会关闭eslint,基于接手的代码用eslint的时候报错太多,强迫症的人实在忍受不了报错,先实行关闭: 1.在build 下面的 webpack.base.conf.js 找到 module- ...
- LDO选型注意事项
以前选择LDO时因为要求不高,只会考虑输入电压Vin,输出电压Vout以及最大输出电流Ioutmax,其他的参数基本不做考虑,后来发现,考虑的太不周到,现在做个笔记记录自己的一些心得. 1.考虑最大输 ...
- [js]变量提升-关于条件
条件函数变量提示于全局中函数变量提升不一样. 条件中: 函数变量提升, 只是声明(现新版本浏览器中) if(g()){ function g() { return true } console.log ...
- [CSS] Frequently used method or solutions for issues
Stick button in right side in html Solution: //In the html <div class="float__button" & ...
- MTSC2019第五届中国移动互联网测试开发大会北京站震撼来袭!
MTSC2019 暨第五届中国移动互联网测试开发大会(Mobile Testing Summit China)是由国内最大的测试开发技术社区之一 TesterHome 发起的行业会议,聚焦于软件测试及 ...
- PHP提交表单失败后如何保留填写的信息
index.html模板文件大内容: <html> <head> <title>jQuery Ajax 实例演示</title> </head&g ...
- 移动端长按响应事件以及阻止默认行为e.preventDefault()导致定时器setTimeout不能响应
手指触摸绑定: $(document).on('touchstart', '.photo', function(e){ currentIndex = parseInt($(this).index('. ...