Given an array S of n integers, are there elements abc in S 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.

For example, given array S = [-1, 0, 1, 2, -1, -4],

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

解法:

  首先对原数组进行排序,然后开始遍历排序后的数组,这里注意不是遍历到最后一个停止,而是到倒数第三个就可以了,中间如果遇到跟前一个数相同的数字就直接跳过。对于遍历到的数,如果大于0则跳到下一个数,因为三个大于0的数相加不可能等于0;否则用0减去这个数得到一个sum,我们只需要再之后找到两个数之和等于sum即可,这样一来问题又转化为了求two sum,这时候我们一次扫描,找到了等于sum的两数后,加上当前遍历到的数字,按顺序存入结果中即可,然后还要注意跳过重复数字。时间复杂度为 O(n2)。代码如下:

public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>(); // 注意不能是new List<>(); List是接口 if (nums == null || nums.length < 3) {
return res;
} Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++) {
if (nums[i] > 0) {
break;
}
if (i > 0 && nums[i] == nums[i - 1]) {
continue;
} int sum = -nums[i];
int left = i + 1, right = nums.length - 1; while (left < right) {
if (nums[left] + nums[right] == sum) {
ArrayList<Integer> triplet = new ArrayList<>();
triplet.add(nums[i]);
triplet.add(nums[left]);
triplet.add(nums[right]);
res.add(triplet); while (left < right && nums[left++] == nums[left]) {}
while (left < right && nums[right--] == nums[right]) {} } else if (nums[left] + nums[right] < sum) {
while (left < right && nums[left++] == nums[left]) {} } else {
while (left < right && nums[right--] == nums[right]) {}
}
}
}
return res;
}
}

[LeetCode] 15. 3Sum ☆☆的更多相关文章

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

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

  2. leetcode 15. 3Sum 二维vector

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

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

  4. LeetCode——15. 3Sum

    一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...

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

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

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

  7. LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum

    n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...

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

  9. leetcode 15 3sum & leetcode 18 4sum

    3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...

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

随机推荐

  1. CryptoZombies学习笔记——Lesson3

    第三课就开始深入讲解solidity编程技巧了. chapter1: 智能合约的不变性. 合约一旦部署到以太坊后,就不可更改了,所以从一方面来说,智能合约代码的安全性是如此重要,因为一旦发现你的代码里 ...

  2. 第一次接触FPGA至今,总结的宝贵经验

    从大学时代第一次接触FPGA至今已有10多年的时间,至今记得当初第一次在EDA实验平台上完成数字秒表.抢答器.密码锁等实验时那个兴奋劲.当时由于没有接触到HDL硬件描述语言,设计都是在MAX+plus ...

  3. ubuntu apache nginx 启动 关闭

    转载自:http://www.comflag.com/2011/05/01/apache-web.htm 电影<社交网络>中,facebook创始人马克.扎克失恋后入侵哈佛大学宿舍楼服务器 ...

  4. HTTP 请求头 & 响应头

    HTTP请求头概述 HTTP客户程序(例如浏览器),向服务器发送请求的时候必须指明请求类型(一般是GET或者POST).如有必要,客户程序还可以选择发送其他的请求头.大多数请求头并不是必需的, 但Co ...

  5. LintCode-380.两个链表的交叉

    两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 注意事项 如果两个链表没有交叉,返回null. 在返回结果后,两个链表仍须保持原有的结构. 可假定整个链表结构中没有循环. 样例 下列两 ...

  6. MATLAB中imfilter函数

    功能:对任意类型数组或多维图像进行滤波. 用法:B = imfilter(A,H) B = imfilter(A,H,option1,option2,...) 或写作g = imfilter(f, w ...

  7. BZOJ 1221 软件开发(费用流)

    容易看出这是显然的费用流模型. 把每天需要的餐巾数作为限制.需要将天数拆点,x’表示每天需要的餐巾,x’’表示每天用完的餐巾.所以加边 (s,x',INF,0),(x'',t,INF,0). 餐巾可以 ...

  8. BZOJ4735 你的生命已如风中残烛(组合数学)

    将每个位置上的数都-1,则显然相当于前缀和始终非负. 然后就是完全想不到的了.考虑往里面加一张-1的牌.假设在一个合法排列的最后添上一个-1,那么在该排列的所有循环同构排列中,满足前m个前缀和都非负的 ...

  9. Python使用requests模块下载图片

    MySQL中事先保存好爬取到的图片链接地址. 然后使用多线程把图片下载到本地. # coding: utf-8 import MySQLdb import requests import os imp ...

  10. 容器化RDS|计算存储分离 or 本地存储?

    随着交流机会的增多(集中在金融行业,规模都在各自领域数一数二),发现大家对 Docker + Kubernetes 的接受程度超乎想象, 并极有兴趣将这套架构应用到 RDS 领域.数据库服务的需求可以 ...