Leetcode题库——15.三数之和
@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.三数之和的更多相关文章
- Leetcode题库——1.两数之和
@author: ZZQ @software: PyCharm @file: addTwoNumbers.py @time: 2018/9/18 10:35 要求:给定两个非空链表来表示两个非负整数. ...
- LeetCode 15. 三数之和(3Sum)
15. 三数之和 15. 3Sum 题目描述 Given an array nums of n integers, are there elements a, b, c in nums such th ...
- Java实现 LeetCode 15 三数之和
15. 三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以 ...
- 代码随想录第七天| 454.四数相加II、383. 赎金信 、15. 三数之和 、18. 四数之和
第一题454.四数相加II 给你四个整数数组 nums1.nums2.nums3 和 nums4 ,数组长度都是 n ,请你计算有多少个元组 (i, j, k, l) 能满足: 0 <= i, ...
- leetcode题目15.三数之和(中等)
题目描述: 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重 ...
- LeetCode——15. 三数之和
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复的三元组. ...
- 【LeetCode】15.三数之和
题目描述 1. 三数之和 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?请你找出所有满足条件且不重复的三元组. 注意: ...
- [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 < ...
- [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 ...
随机推荐
- 在element-ui的select下拉框加上滚动加载
在项目中,我们需要运用到很多来自后端返回的数据.有时是上百条,有时甚至上千条.如果加上后端的多表查询或者数据量过大,这就导致在前端的显示就会及其慢,特别是在网络不好的时候更是如此. 自然,后端就做了一 ...
- 洛谷P1028动规算法
首先我们可以写一个递归 #include<bits/stdc++.h> using namespace std; long long n; int main(){ long long f[ ...
- MySql修改WordPress密码
首先,需要几样东西:Xshell,MySql的密码 具体过程:这里我的服务器用的是Vultr, 登上数据库: 使用的语句是:mysql -u root -p 出现的结果如下图: 接着输入语句:show ...
- C语言学习记录_2019.02.03
优先级:算术运算符 > 关系运算符 > 赋值 ==和!=的优先级低于其他关系运算符 连续的关系运算符从左到右进行 注释:“//”或“/**/” 判断语句:if else 写代码有时看重的是 ...
- Scala的映射和元组操作
映射和元组操作 构造Map // 构造一个不可变的MAP映射,类似与key -> value这样的组合叫做对偶 val score = Map("Jack" -> 12 ...
- 安装好XAMPP+安装好PhpStorm 然后搭建PHP开发环境
1.安装XAMPP 1.1.可以参考我的这篇博客:XMAPP的安装与配置. 2.安装并破解PhpStorm 2.1.可以参考我的这篇博客:PhpStorm2016.2版本安装与破解. 3.配置PhpS ...
- JavaWeb总结(十)
Filter配置详解 web项目目录示意图 <!-- Filter配置 --> <filter> <display-name>Filter_one</disp ...
- 如何查看win2003是32位还是64位
如何查看自己的电脑是32位还是64位 方法如下: 点击开始——运行——输入wmic cpu get addresswidth
- 安装OpenLDAP步骤
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL杂记页 回到顶级页面:PostgreSQL索引页 [作者 高健@博客园 luckyjackgao@gmail. ...
- python基础学习1-迭代器
#!/usr/bin/env python # -*- coding:utf-8 -*- #自定义迭代器 需要 重写 __iter__()和__next__() 两个魔法方法 class Fibs: ...