@author: ZZQ

@software: PyCharm

@file: threeSum.py

@time: 2018/10/6 19:47

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

注意:答案中不可以包含重复的三元组。

例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],

满足要求的三元组集合为:

[

[-1, 0, 1],

[-1, -1, 2]

]

思路:先对数组排序,然后找满足 nums[first] + nums[last] + nums[middle] = 0 的三个数。同时去掉重复的三元组。

class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
out_index = []
if len(nums) == 0:
return []
nums.sort()
for middle in range(len(nums)-2):
if middle == 0 or nums[middle] > nums[middle-1]:
first = middle + 1
last = len(nums) - 1
while first < last:
if nums[first] + nums[last] + nums[middle] == 0:
out_index.append([nums[first], nums[middle], nums[last]])
first += 1
last -= 1
while nums[first] == nums[first-1] and first < last:
first += 1
while nums[last] == nums[last+1] and first < last:
last -= 1
elif nums[first] + nums[last] + nums[middle] < 0:
first += 1
else:
last -= 1
return out_index

Leetcode题库——15.三数之和的更多相关文章

  1. Leetcode题库——1.两数之和

    @author: ZZQ @software: PyCharm @file: addTwoNumbers.py @time: 2018/9/18 10:35 要求:给定两个非空链表来表示两个非负整数. ...

  2. LeetCode 15. 三数之和(3Sum)

    15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...

  3. Java实现 LeetCode 15 三数之和

    15. 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...

  4. 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和

    第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...

  5. leetcode题目15.三数之和(中等)

    题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重 ...

  6. LeetCode——15. 三数之和

    给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...

  7. 【LeetCode】15.三数之和

    题目描述 1. 三数之和 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意: ...

  8. [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 < ...

  9. [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 ...

随机推荐

  1. mac上cocoapods安装与卸载

    安装 # 安装最新beta版 sudo gem install cocoapods --pre -n /usr/local/bin # 安装最新稳定版 sudo gem install cocoapo ...

  2. 20155332 补交课后测试——ch11网络编程

    20155332 补交课后测试--ch11网络编程 这章的课后测试忘了提交,我课后补做了这章的测试题目,并将知识点和自己的错题汇总如下: 本章知识点总结 11.1 客户端-- 服务器模型 每个网络应用 ...

  3. 20155338《网络对抗技术》 Exp1 PC平台逆向破解

    20155338<网络对抗技术> Exp1 PC平台逆向破解 实践目标 1.实践的对象是一个名为pwn1的linux可执行文件. 2.该程序正常执行流程是:main调用foo函数,foo函 ...

  4. Spring boot ---- java.lang.NoClassDefFoundError: javax/servlet/ServletContext

    Spring boot ---- java.lang.NoClassDefFoundError: javax/servlet/ServletContext   场景描述 项目中用到spring boo ...

  5. 微信小程序的经纬度不想写死,需要转成number类型不能用浮点型

    click: function (e) { var msg = this.data.placeData; var latitude = Number(msg.latitude) var longitu ...

  6. 仙人掌&圆方树

    仙人掌&圆方树 Tags:图论 [x] [luogu4320]道路相遇 https://www.luogu.org/problemnew/show/P4320 [ ] [SDOI2018]战略 ...

  7. 4-[HTML]-body常用标签1

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. Redis架构之防雪崩设计:网站不宕机背后的兵法

    互联网系统中不可避免要大量用到缓存,在缓存的使用过程中,架构师需要注意哪些问题?本文以 Redis 为例,详细探讨了最关键的 3 个问题. 一.缓存穿透预防及优化 缓存穿透是指查询一个根本不存在的数据 ...

  9. Lookup 转换组件

    查找转换(Lookup)组件用于实现两个数据源的连接,实现的方式是嵌套循环.查找转换通常在内存中缓存查找数据集,然后在输入管道中,把输入数据的每一行都和缓存中的查找数据集进行比较,并输出匹配成功和失败 ...

  10. 【Java源码解析】Thread

    简介 线程本质上也是进程.线程机制提供了在同一程序内共享内存地址空间运行的一组线程.对于内核来讲,它就是进程,只是该进程和其他一下进程共享某些资源,比如地址空间.在Java语言里,Thread类封装了 ...