LeetCode--015--三数之和(python)
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。

先对nums进行排序,再用双指针,L=i+1,R=len(nums)-1,i从索引0开始遍历,until nums[i]>0退出
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
if(not nums or len(nums)<3):
return []
nums.sort()
res = []
for i in range(len(nums)):
L = i+1
R = len(nums)-1
if nums[i]>0:
return res
if i > 0 and nums[i-1]==nums[i]:
continue
while L <R:
if nums[i]+nums[L]+nums[R]==0:
res.append([nums[i],nums[L],nums[R]])
while L < R and nums[L]==nums[L+1]:
L+=1
while L <R and nums[R]==nums[R-1]:
R-=1
L+=1
R-=1
elif nums[i]+nums[L]+nums[R]>0:
R-=1
else:
L+=1
return res
时间复杂度:O(N^2) = 数组排序O(NlogN)+遍历数组O(N)*双指针遍历O(N)
空间复杂度:O(1)
LeetCode--015--三数之和(python)的更多相关文章
- LeetCode:三数之和【15】
LeetCode:三数之和[15] 题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的 ...
- [LeetCode] 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】三数之和【排序,固定一个数,然后双指针寻找另外两个数】
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- LeetCode 15. 三数之和(3Sum)
15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...
- LeetCode——15. 三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- Java实现 LeetCode 15 三数之和
15. 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...
- LeetCode 15. 三数之和(3Sum)
题目描述 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...
- [Leetcode 15]三数之和 3 Sum
[题目] Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? ...
- [LeetCode] 15. 三数之和
题目链接:https://leetcode-cn.com/problems/3sum/ 题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a ...
- [LeetCode]15. 三数之和(数组)(双指针)
题目 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三 ...
随机推荐
- hibernate 2 多对多映射
一.实体类 1.Classes.java package cn.gs.wwg.entity; import java.util.Set; public class Classes { private ...
- 一个JSON解析器
来源 <JavaScript语言精粹(修订版)> 代码 <!DOCTYPE html> <html> <head> <meta charset=& ...
- Matlab——图形绘制——三维立体图形 剔透玲珑球 动态图——彗星状轨迹图
三维绘图函数 三维绘制工具 函数view 实例:三维螺旋线 >> t=:pi/:*pi; plot3(sin(t),cos(t),t) grid %添加网格 plot3可以画出空间中的曲 ...
- excel 导入导出测试点
目前,为方便操作,很多系统都会增加批量导入导出的功能.文件导入导出一般格式都是excel.由于用户直接在excel在填写内容,无法控制填写的格 式,加上excel解析比较困难,所以一般涉及到excel ...
- EFI系统分区如何删除
U盘或者硬盘被做了系统安装盘. 结果在格式化都是失败,分区也不行. 有了新招 EFI分区是您的系统启动引导的分区,存放引导启动的文件的,因此它是一个操作系统独立的分区,实际上它是UEFI加载的固件和应 ...
- 【监控笔记】【2.3】扩展事件——慢查询SQL(执行超过3S的SQL)
--sql server 2008及以上才支持,2012及以上才支持GUI界面 msdn 扩展事件:点击打开链接 [1]T-SQL实现 基于 rpc_completed(远程过程调用已完成时发生) 事 ...
- PL/SQL基本操作
1.常规过程化形式 declare o_booking_flag ); begin -- Call the procedure destine_ticket(', , 'E', , o_booking ...
- Kotlin学习(5)类型系统
可空性(避免空指针异常) /* *这个函数的参数代表传入一个String类型变量的实例,这代表它不可以为空 */ fun a(str:String){ println(str) } //这样调用a() ...
- LNMP环境搭建哈哈
经过一番折腾,终于将LNMP环境搭建完成了.本文介绍的LNMP环境是在windows的Oracle VM VirtualBox中的Centos虚拟机上搭建的,各个软件的版本为:Centos7 + Ng ...
- JavaScript是如何工作的:引擎,运行时间以及调用栈的概述
JavaScript是如何工作的:引擎,运行时以及调用栈的概述 原文:How JavaScript works: an overview of the engine, the runtime, and ...