描述

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.

Example:

Given array nums = [-1, 0, 1, 2, -1, -4],

A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]

解析

先排序,再确定一个数字,用后面的数据获取0 - num[i]的值,转成 2sum的问题。要注意跳过重复数据。

代码

解法1(解析所述)

public static List<List<Integer>> threeSum3(int[] num) {
if (num == null || num.length < 3) {
return new ArrayList<>();
}
Arrays.sort(num);
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < num.length - 2; i++) {
if (i == 0 || (i > 0 && num[i] != num[i - 1])) {
int target = 0 - num[i];
int start = i + 1;
int end = num.length - 1;
while (start < end) {
if (num[start] + num[end] == target) {
res.add(Arrays.asList(num[i], num[start], num[end]));
//去除重复元素
while (start < end && num[start] == num[start + 1]) {
start++;
}
while (start < end && num[end] == num[end - 1]) {
end--;
}
start++;
end--;
} else if (num[start] + num[end] > target) {
end--;
} else {
start++;
}
}
}
}
return res;
}

数组中找等于指定数的集合

也可以用数组num,在其中找n个数等于指定数的解法。

[LeetCode] 15. 3Sum ☆☆☆(3数和为0)的更多相关文章

  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. 3Sum三数之和

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

  3. leetCode 15. 3Sum (3数之和) 解题思路和方法

    3Sum  Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...

  4. LeetCode 15 3Sum [sort] <c++>

    LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...

  5. 【LeetCode】15. 3Sum 三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...

  6. leetcode 15. 3Sum 二维vector

    传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...

  7. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

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

  9. LeetCode 15 3Sum(3个数求和为0的组合)

    题目链接 https://leetcode.com/problems/3sum/?tab=Description   Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...

随机推荐

  1. No WebApplicationContext found: no ContextLoaderListener registered

    修改前运行报错:No WebApplicationContext found: no ContextLoaderListener registered? <web-app> <dis ...

  2. ElasticSearch文档删除字段

    https://www.cnblogs.com/ljhdo/archive/2017/03/24/4885796.html

  3. MongoDB集群之分片技术应用 —— 学习笔记

    课程链接:https://www.imooc.com/learn/501 一.什么是分片? 分片:将数据进行2拆分,将数据水平的分散到不同的服务器上. 二.为什么要分片? 架构上:读写均衡.去中心化 ...

  4. 【c# 学习笔记】阻止派生类重写虚成员

    使用sealed 关键字可以防止一个类被其他类继承.同样,也可以使用sealed关键字来阻止派生类重写虚成员.如,我们希望Horse的继承类不再具有扩展Voice方法的行为.(上一章链接:https: ...

  5. 高级UI-RecyclerView拖拽和侧滑

    RecyclerView强大的地方在于高度的可定制,正式由于此优点,现在的项目大多使用RecyclerView,这里我们仿照QQ的功能,实现RecyclerView的拖拽和侧滑功能 功能说明 上下拖拽 ...

  6. vue和小程序的相似之处

    小程序参考vue语法,之前做过小程序的,可以逆向思维.1,Vue文件后缀是.vue,vue组件把html<template>.js<script>和css<style&g ...

  7. Asp.Net Core 客户端验证和远程验证

    我们先来看这样一个注册页面和它的后台Model @model RegisterViewModel @{ ViewBag.Title = "用户注册"; } <h1>用户 ...

  8. 如何制作windows live writer绿色便携版

    如何制作windows live writer绿色便携版 2013年10月03日 ⁄ 综合 ⁄ 共 463字 ⁄ 字号 小 中 大 ⁄ 评论关闭 制作一个绿色便携版的wlw 1.首先下载 techli ...

  9. 一个后端开发者的前端语言基础:JavaScript

    JavaScript (一) 基本概述 (1) 概述 JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为浏览器的 ...

  10. WCF-简单 配置文件

    一.服务端配置文件 主要包括 1.services 配置服务节点 <!--name 指的是契约实现类--> <service name="WcfLib.User2" ...