题目描述:

中文:

给定一个包含 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. git,提交错了分支,想把远程的分支恢复到上一个版本

    1.先将本地分支回滚到上一个版本 2.删除远程分支(可以先备份一下) 3.创建新的分支,将本地分支push上去

  2. SQL Server 2008 R2 数据库备份文件.bak如何挂载到【阿里云&#183;独立虚拟主机数据库】上

    1.前言 8月份刚刚开始,公司[工作流]挂了,实体服务器会自动重启,数据库死活起不来,这可是计算工资提成的事儿,不能马虎! 在实体服务器中,找到了OA页面源码与数据库的.MDF 与 .LDF 等文件. ...

  3. Juint test Case 的2种使用方式

    通常情况下,我们去测试一个类中的方法,首先是建立一个包,包中建立一个测试类,在建立测试类文件时,选择JUnit Test Case,如下: 建好之后写测试用例: 但是如果偏就想在编写方法的那个java ...

  4. fileupload组件之上传与下载的页面

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  5. 4412 搭建tftp服务器

    搭建服务器 --安装xinetd,sudo apt-get install xinetd --安装tftp和tftpd,sudo apt-get install tftp tftpd --配置/etc ...

  6. ffmpeg+nginx-rtmp-module

    原址: https://www.cnblogs.com/cnsanshao/p/6370938.html另外: vlc播放器能播放rtsp协议 nginx安装和配置 模块下载 https://gith ...

  7. [CSP-S模拟测试]:养花(分块)

    题目描述 小$C$在家种了$n$盆花,每盆花有一个艳丽度$a_i$.在接下来的$m$天中,每天早晨他会从一段编号连续的花中选择一盆摆放在客厅,并在晚上放回.同时每天有特定的光照强度$k_i$,如果这一 ...

  8. [CSP-S模拟测试]:次芝麻(数学)

    题目描述 小$K$和小$X$都是小次货.身为小次货,最重要的事情就是次啦!所以他们正在纠结如何分芝麻次.一开始,小$K$有$n$个芝麻,小$X$有$m$个芝麻.因为他们都想次更多芝麻,所以每次手中芝麻 ...

  9. 119、TensorFlow如何创建计算图

    #Dataflow是并行化编程常用的方式 #当使用TensorFlow执行你的程序的时候有以下几个优点 #1.并行化 .通过声明的边来代表操作之间的依赖 # 对系统来说确定可以并行化的操作是比较容易的 ...

  10. jenkins实现手动选择分支构建项目-Git Paramater

    先下载插件: Git Paramater 参照: jenkins-参数化构建(三)插件:Git Parameter https://www.cnblogs.com/zhaojingyu/p/98624 ...