一、题目

给你一个包含 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. PHP PSR-2 代码风格规范

    代码风格规范 本篇规范是 PSR-1 基本代码规范的继承与扩展. 本规范希望通过制定一系列规范化PHP代码的规则,以减少在浏览不同作者的代码时,因代码风格的不同而造成不便. 当多名程序员在多个项目中合 ...

  2. LG承认手机业务遭到中国厂商碾压!这是输得心服口服的节奏?

    近日,关于LG手机业退出中国市场的消息传的沸沸洋洋.不少相关媒体也对此事向LG北京办事处求证,得到的结果确实是手机业务退出中国市场.并且据韩媒报道,LG还将会逐渐取消高端手机业务,也就是说未来V系列和 ...

  3. 吴裕雄--天生自然 Tensorflow卷积神经网络:花朵图片识别

    import os import numpy as np import matplotlib.pyplot as plt from PIL import Image, ImageChops from ...

  4. Linux命令alias - 设置命令的别名

    用途说明设置命令的别名.在linux系统中如果命令太长又不符合用户的习惯,那么我们可以为它指定一个别名.虽然可以为命令建立“链接”解决长文件名的问题,但对于带命令行参数的命令,链接就无能为力了.而指定 ...

  5. IDEA 详细使用教程

    第一步:安装 略 安装首先需要Java语言的运行环境,这里不做概述. 第二步:破解 这里使用的是2016版的,所以直接在网上搜索秘钥即可 https://blog.csdn.net/ksksjipen ...

  6. ES Templates push

    { "order": 0, "template": "connector-connection*", "settings" ...

  7. mysql 分表实现方法详解

    如果你需要进行mysql分表了我们就证明你数据库比较大了,就是把一张表分成N多个小表,分表后,单表的并发能力提高了,磁盘I/O性能也提高了.并发能力为什么提高了呢,因为查寻一次所花的时间变短了,如果出 ...

  8. C++走向远洋——35(友元,时间)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:time.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  9. 【ThinkPHP6:从TP3升级到放弃】1. 前言及准备工作

    春节期间因为疫情的关系出不去门,所以就研究了一下ThinkPHP的最新版本6.0.2, 自己写了一个博客程序. 现在, 打算写一个ThinkPHP6的专题, 用来把自己在写博客的过程中入过的坑和获得的 ...

  10. Spring编译后没有xml配置文件解决方法

    问题描述 在使用Maven来构建Spring项目的时候,使用下面代码来读取Spring配置文件. ClassPathXmlApplicationContext context = new ClassP ...