题目描述:

中文:

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

英文:

Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

The solution set must not contain duplicate triplets.

class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
result = []
if len(nums) < 3:
return result
nums.sort()
for i in range(0, len(nums)):
j = i + 1
k = len(nums) - 1
if i > 0 and nums[i] == nums[i - 1]:
continue #
while j < k:
sum = nums[i] + nums[j] + nums[k]
if sum == 0:
result.append((nums[i], nums[j], nums[k]))
k -= 1
j += 1
while(nums[j] == nums[j - 1] and nums[k] == nums[k + 1] and j < k):
j += 1
elif sum > 0:
k -= 1
while(nums[k] == nums[k + 1] and j < k):
k -= 1
else:
j += 1
while(nums[j] == nums[j - 1] and j < k):
j += 1
return list(set(result))

题目来源:力扣

力扣 ——3Sum python (三数之和)实现的更多相关文章

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

  2. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  3. python三数之和

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

  4. 力扣 ——4Sum (四数之和)python 实现

    题目描述: 中文: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 targe ...

  5. 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 all un ...

  6. LeetCode第[15]题(Java):3Sum (三数之和为目标值)——Medium

    题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c  ...

  7. 15. 3Sum[M]三数之和

    题目 Given an array nums of n integers, are three elements a, b, c in nums such that a+b+c=0? Find all ...

  8. 3sum 求三数之和等于0,不允许重复

    https://leetcode.com/problems/3sum/ 套路比较常见了,最重要的是去重.还是没法一次通过. class Solution { public: vector<vec ...

  9. 259 [LeetCode] 3Sum Smaller 三数之和较小值

    题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...

随机推荐

  1. ubuntu在线搭建ftp服务器

    转载:https://www.linuxidc.com/Linux/2016-12/138563.htm 在Linux中ftp服务器的全名叫 vsftpd,我们需要利用相关命令来开启安装ftp服务器, ...

  2. elasticsearch删除

    1.根据id删除 2.根据查询条件删除

  3. 注意!黑客可以通过CSS3功能攻击浏览器

    随着通过HTML5和CSS3引入的惊人数量的功能,浏览器的攻击面也相应增长.因此,这些功能之间的交互可能会导致意外行为影响用户的安全,这并不奇怪.在这篇文章中,中国知名黑客安全组织东方联盟描述了这样一 ...

  4. CF840E In a Trap

    题意:给你一棵节点带权树.q个询问,每次询问u到v的路径上max(a[i]^dis(i,v))? 保证u是v的祖先,i是u->v路径上的点.n,ai<=5e4. 标程: #include& ...

  5. 英语单词contributors

    contributors 来源——github网站 翻译 n. 贡献者:参与者:编著者(contributor的复数形式) TOEFL | GMAT                   词根:cont ...

  6. 一次OOM测试并分析

    堆dump文件分析 Java代码: public class HeapTest2 { static class OOMObject { private byte[] arrs = new byte[6 ...

  7. Spring学习总结(2)- AOP

    一,什么是AOP AOP(Aspect Oriented Programming)意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中 ...

  8. 十条服务器端优化Web性能的技巧

    服务器  远程桌面连接工具 提高web应用的性能从来没有比现在更重要过.网络经济的比重一直在增长;全球经济超过5%的价值是在因特网上产生的(数据参见下面的资料).这个时刻在线的超连接世界意味着用户对其 ...

  9. pytest_用例运行级别_模块级

    ''' pytest 参数说明 https://www.jianshu.com/p/7a7432340f02 -x test_fixt_model.py 遇到错误时,停止运行 用-v运行(-v显示运行 ...

  10. I2C总线协议详解

    I2C总线定义     I2C(Inter-Integrated Circuit)总线是一种由PHILIPS公司开发的两线式串行总线,用于连接微控制器及其外围设备.I2C总线产生于在80年代,最初为音 ...