题目描述:

中文:

给定一个包含 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. javascript 回到顶部效果的实现

    demo.js window.onload=function() { var timer=null; var obtn=document.getElementById('btn'); var isTo ...

  2. vue代理配置之二--dev方式启动的index.js配置

    'use strict' // Template version: 1.3.1 // see http://vuejs-templates.github.io/webpack for document ...

  3. Fiddler的详细介绍

    Fiddler的详细介绍 一.Fiddler与其他抓包工具的区别 1.Firebug虽然可以抓包,但是对于分析http请求的详细信息,不够强大.模拟http请求的功能也不够,且firebug常常是需要 ...

  4. Bootstrap的本地引入

    今天用前端框架时选择了Bootstrap,然后东西都下好了本地就是引入不进去. 查了一下发现必须jquery要在BootStrap之前引入,然后我更改了引入顺序,发现还是不行 <script s ...

  5. leetcode简单刷题

    [python3]参数中的冒号与箭头 冒号后面是建议传入的参数类型 箭头后面是建议函数返回的类型

  6. Oracle分组函数之ROLLUP

    功能介绍: 首先是进行无字段的聚合,然后在对字段进行从左到右依次组合后聚合 创建表: Create Table score ( classID Int, studentName ), subject ...

  7. php str_pad()函数 语法

    php str_pad()函数 语法 str_pad()函数怎么用? php str_pad()函数用于把字符串填充到指定长度,语法是str_pad(string,length,pad_string, ...

  8. Django-template模板语言

    一.常用语法 只需要记两种特殊符号: {{  }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 首先把views里代码贴出了,之后就是在HTML中各种模板语言替换了 本质都是字符串的替 ...

  9. Dllregisterserver调用失败解决方法

    在做一个注册com组件时,出现这样的情况 出现这个错误一般是和权限问题有关,命令提示符需要以管理员权限运行才可以注册成功. 最简单的解决方法就是: 在开始菜单右击—>命令提示符(管理员)(A) ...

  10. 【项目管理和构建】——Maven简介(一)

    在现实的企业中,以低成本.高效率.高质量的完成项目,不仅仅需要技术大牛,企业更加需要管理大牛,管理者只懂技术是远远不够的.当然,管理可以说有很多的方面,例如:对人员的管理,也有对项目的管理等等.如果你 ...