一、题目

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

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

示例:

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

满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]

二、题解

思路1:

之前有道算法题,是求两数之和,所以想法就是先遍历数组,固定当前元素,并求出当前元素的相反数,即剩下两个数的和sum;然后转化成 twoSum;最后去重。

本地跑是没问题的,但是,在力扣上提交代码,等待系统判分后,执行结果是超出时间限制——系统给的输入是好大一段数组,里面得有几百个数字吧?!

LeetCode#15 | Three Sum 三数之和的更多相关文章

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

  2. LeetCode(15):三数之和

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

  3. leetcode第15题:三数之和

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

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

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

  6. [LeetCode] 1. Two Sum 两数之和

    Part 1. 题目描述 (easy) Given an array of integers, return indices of the two numbers such that they add ...

  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. [LeetCode]1.Two Sum 两数之和&&第一次刷题感想

    ---恢复内容开始--- 参考博客: https://www.cnblogs.com/grandyang/p/4130379.html https://blog.csdn.net/weixin_387 ...

  9. [leetcode]1. Two Sum两数之和

    Given an array of integers, return indices  of the two numbers such that they add up to a specific t ...

随机推荐

  1. python学习笔记(6)数据类型-集合

    集合(set)是一个无序的不重复元素序列. 可以使用大括号 { } 或者 set() 函数创建集合,注意:创建一个空集合必须用 set() 而不是 { },因为 { } 是用来创建一个空字典. 创建格 ...

  2. [洛谷P3806] [模板] 点分治1

    洛谷 P3806 传送门 这个点分治都不用减掉子树里的了,直接搞就行了. 注意第63行 if(qu[k]>=buf[j]) 不能不写,也不能写成>. 因为这个WA了半天...... 如果m ...

  3. [LC] 746. Min Cost Climbing Stairs

    On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...

  4. Node.js知识点详解(一)基础部分

    转自:http://segmentfault.com/a/1190000000728401 模块 Node.js 提供了exports 和 require 两个对象,其中 exports 是模块公开的 ...

  5. MAVEN实现多环境搭建

    在实际的开发中,会遇到开发环境的不同(开发环境,测试环境,线上环境),会来回根据环境的不同修改配置文件,一不小心修改错误导致无法正常运行,故障排除导致开发效率低.使用maven可以根据环境的不同,自动 ...

  6. python3多进程爬虫(第一卷)

    进程这个东西概念很多人很模糊,而多进程并发爬虫也算是爬虫几大难点,现在我先说下进程的基本使用: 开启一个python文件 函数sayhi就是一个进程而且是主进程 现在呢我想循环输出NAME,n 正常我 ...

  7. Jenkins 2 如何使用 PowerShell 以及自定 build fail (指定 exit code)

    Jenkins 除了用來做為 CI(持續性整合) 工具外,也可以與其他 plugin 配合達成其他目的(e.g.IIS restart.檔案壓縮備份-),今天就來看看可以怎麼與 PowerShell ...

  8. 吴裕雄--python学习笔记:sqlite3 模块

    1 sqlite3.connect(database [,timeout ,other optional arguments]) 该 API 打开一个到 SQLite 数据库文件 database 的 ...

  9. js new 与 return

    前置: 默认情况下, 函数的返回值是 undefined (即没有定义返回值). new 操作符 js 中的 new 操作符,可以是我们像 java 一样,获得一个新的对象,例如: function ...

  10. virtualenv 个人指南

    virtualenv是解决一个机器上多个应用需要的Python版本不一致的问题,virtualenv就是用来为一个应用创建一套"隔离"的Python运行环境,解决了解决了不同应用间 ...