描述

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. Qt编写控件属性设计器8-网络采集

    一.前言 上一篇文章已经打通了数据源之一的串口采集,这次要说的是网络采集,网络通信目前用的最多的是三种,TCP/UDP/HTTP,其中tcp通信又包括了客户端服务端两种,tcp通信才用了多次握手机制不 ...

  2. CDS视图篇 1

    CDS视图概览 CDS是Core Data Services的简称,是HANA数据库向上层ABAP应用层提供数据的一种高效的方式,CDS模型是基于数据库data definition language ...

  3. html5调整音频音高

    var audioSrc = 'https://s3-us-west-2.amazonaws.com/s.cdpn.io/1715/the_xx_-_intro.mp3'; function buff ...

  4. iOS推送证书上传(转)

    iOS 推送证书制作(JAVA/PHP) 在使用Java或者PHP制作iOS推送服务器的时候,需要自己从开发者网站上导出的aps_developer_identity证书和Apple Developm ...

  5. Oracle数据同步交换

    一.为了解决数据同步汇聚,数据分发,数据转换,数据维护等需求,TreeSoft将复杂的网状的同步链路变成了星型数据链路.     TreeSoft作为中间传输载体负责连接各种数据源,为各种异构数据库之 ...

  6. Egret入门学习日记 --- 第九篇(书中 2.7~2.8节 内容)

    第九篇(书中 2.7~2.8节 内容) 昨天记录到了 2.6节 ,那么今天就从 2.7节 开始. 这个 2.7节 有7个小段,有点长,总结一下重点: 1.调试项目的两种方法. 2.运行项目的两种窗口选 ...

  7. Composer 笔记

    composer 依赖于git而设计的代码仓管理工具 1.可以通过手动配置源,获取代码 "require": { "group/val": "0.0. ...

  8. iostat的坑

    简单使用iostat查询io使用量,会让你看不懂所以然,因为很多人疏忽了这个命令查到的结果根本不是实际值,需要注意的是一句话: “第1次采样信息与单独执行iostat的效果一样,为从系统开机到当前执行 ...

  9. ELK优化难题解决

    你头疼的ELK难题,本文几乎都解决了 一.ELK实用知识点总结 1.编码转换问题 这个问题,主要就是中文乱码. input中的codec=>plain转码: codec => plain ...

  10. SQL Server直接执行.sql文件

    SQL Server直接执行.sql文件    客户的数据库数据被篡改,利用Log Explorer工具根据日志生成的回滚脚本有200多M,不可能一下子扔到查询分析器里去执行,于是想是否SQL Ser ...