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. anaconda安装scrapy报错解决办法

    今天在用anaconda安装scrapy的时候遇见个坑,现在将解决办法发出来,供大家参考使用: 问题描述: anaconda安装scrapy,使用 conda install scrapy 命令.安装 ...

  2. c#积累之测试

    初来上班,免不了看别人代码.快速搞懂别人代码是我现在受到的一大挑战.寻摸着规律,发现一边进行调试,一边进行行行注释的逻辑判断不失为一种妙招. c#调试用的是vs2012.f11键和f10和f5键的应用 ...

  3. MyBatis传入参数为list、数组、map写法(转载)

    MyBatis传入参数为list.数组.map写法 1.foreach简单介绍: foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有item ...

  4. 2019寒假训练营寒假作业(二) MOOC的网络空间安全概论笔记部分

    视频课程--MOOC的网络空间安全概论笔记 第一章 网络空间安全概述 2001年,网络空间概念被首次提出: 网络空间安全框架: 1.设备层安全: 可通过截获电磁辐射获取计算机信息.通过硬件木马(恶意电 ...

  5. Alpha 冲刺报告(3/10)

    Alpha 冲刺报告 队名:洛基小队 峻雄(组长) 已完成:开始编写角色的移动脚本 明日计划:继续学习并进行脚本编写 剩余任务:物品背包交互代码 困难:如何把各个模块的脚本整合起来 --------- ...

  6. PokeCats开发者日志(十)

      现在是PokeCats游戏开发的第三十三天的中午,收到了中国版权保护中心软件登记部发来的受理通知书.   上易版权看一眼,貌似离拿证不远了.   想一想还有点小激动呢!

  7. window service 创建

    1:vs中创建一个 window servece 2.右键 添加安装程序 3.更改属性视图中的Account属性为LocalService(本地服务) 更改ServiceName为你自己的服务名称   ...

  8. linux路由表的配置

    linux路由表的配置 一.原理说明 1.路由表(table)从0到255进行编号,每个编号可以对应一个别名,编号和别名的对应关系在linux下放在/etc/iproute2/rt_tables这个文 ...

  9. 【Python】python基础_代码编写注意事项

    1. 说明使用的编译方式 1 #!/usr/bin/python 2. 说明字符编码方式 1 #coding=utf-8 3. print 默认输出是换行的,如果要实现不换行需要在变量末尾加上逗号 # ...

  10. WIN7系统插入蓝牙适配器经常断开问题

    WIN7 ACER笔记本一台,蓝牙耳机一个,10块钱的蓝牙适配器一个 目的:可以在笔记本上用适配器与蓝牙耳机匹配 出现问题:1.有2个图标,一会左边感叹号,一会右边感叹号,必须有个存在感叹号 解决:第 ...