描述

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. 好用的开源SVN仓库

    1.地址 https://svnbucket.com/#/projects 2.简单注册使用即可

  2. css简单学习属性3---css属性选择器

    1:通配符 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  3. ABAP字符串操作1 检查字段小数位的长度

    目的: 标准值1-6检查----最多保留小数点后3位 ,如果超出3位,显示错误信息”请检查父件XXX工序XXX的标准值X 的数值XXXX超出3位 “,退出. 关键语法1. SPLIT ,        ...

  4. SecureCRT 8.1破解方式

    百度网盘下载,里面有破解程序和破解方式. 链接: https://pan.baidu.com/s/1wlhqnn-TE_mcOXOLljP-zg 密码: 3ffj

  5. 【Leetcode_easy】806. Number of Lines To Write String

    problem 806. Number of Lines To Write String solution: class Solution { public: vector<int> nu ...

  6. 统一建模语言UML

    目录 1. UML定义 2. UML结构 2.1 视图(View) 2.2 图(Diagram) 2.3 模型元素(Model element) 2.4 通用机制(General mechanism) ...

  7. dozer工具类

    jar:commons-beanutils-1.9.3.jar.commons-lang-2.6.jar.dozer-5.3.2.jar.jcl-over-slf4j-1.7.25.jar.slf4j ...

  8. Flutter FutureBuilder异步请求列表示例

    Flutter的FutureBuilder列表示例 import 'package:flutter/material.dart'; import '../service/service_method. ...

  9. iCMSv7.0.15后台database.admincp文件仍存在SQL注入漏洞

    闲着无聊,国庆时间没事做,又在Q群看到这种公告,只好下个icms慢慢玩.(PS:医院和学校居然都关网站了) 无奈自己太菜,审不出问题.只好上网百度icms之前的漏洞.然后居然成功在iCMSv7.0.1 ...

  10. 前端nginx配置

    对nginx还是处于小白阶段,知道的只是简单基础,以下配置没有问题,已实现 文件:nginx-1.15.11\conf\nginx.conf 注释:# 后台接口 :location ^~ /geste ...