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]
]
分析:拿到题目,暴力for循环,然后就超时了。这是超时的代码。
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
if(nums!=null) {
int max = 0, min = 0;
int index1, index2, index3;
index1 = index2 = index3 = 0;
for (; index1 < nums.length; index1++) {
for (index2 = index1 + 1; index2 < nums.length; index2++) {
for (index3 = index2 + 1; index3 < nums.length; index3++) {
// 如果满足条件合为1
if (nums[index1] + nums[index2] + nums[index3] == 0) {
// 计算三个数中最大值最小值
min = Math.min(Math.min(nums[index1], nums[index2]), nums[index3]);
max = Math.max(Math.max(nums[index1], nums[index2]), nums[index3]);
if (list == null) {
list = new ArrayList<>();
}
list.add(Arrays.asList(min, 0 - min - max, max)); }
}
}
}
for (int i = 0; i < list.size(); i++) {
for (int j = i + 1; j < list.size(); j++) {
if (list.get(i).get(0) == list.get(j).get(0) && list.get(i).get(2) == list.get(j).get(2)) {
list.get(j).set(0, 1);
list.get(j).set(1, 1);
list.get(j).set(2, 1);
}
}
}
for (int i = 0; i < list.size(); i++) {
if (list.get(i).get(0) == 1 && list.get(i).get(1) == 1 && list.get(i).get(2) == 1) {
list.remove(i);
i--;
continue;
}
}
return list;
}else
return list;
}
}

没办法,看看别人的答案。

别人的代码比较简单,想法也很简单,只是优化很好,先排序,然后从头开始选定一个数,再从这个数的后一位和数组最后一位开始向前查找,如果满足和为0的,即添加进list,这里优化的点是,如果一开始选定的这个数在向后移动的过程中,他与前一个数一样,那么就跳过这一次,因为会重复,如果后面两个数在移动过程中也遇到了连续相同的两个数,也跳过这次,保证不重复。

public static List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
Arrays.sort(nums); for (int i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] == nums[i - 1])
continue;
int target = -nums[i];
int lo = i + 1;
int hi = nums.length - 1;
while (lo < hi) {
if (nums[lo] + nums[hi] == target) {
list.add(Arrays.asList(nums[i], nums[lo], nums[hi]));
lo++;
hi--;
while (lo<hi&&nums[lo] == nums[lo-1])
lo++;
while (lo<hi&&nums[hi] == nums[hi+1])
hi--;
} else if (nums[lo] + nums[hi] > target) {
hi--;
} else {
lo++;
}
}
}
return list;
}

还有一个比较奇葩的错误,中间声明的三个变量target/lo/hi,如果我拿到for语句外面声明,在里面赋值,就超时了,拿进来声明并赋值就可以通过,估计是声明和赋值分开操作会比较耗时吧。

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. 移动端web开发安卓和ios客户端在时间转换上的差异性问题

    作为一名移动前端开发的人员,平时遇到的兼容性问题不在少数.那么,今天就来说一下最近遇到的一个小坑(关于Android和ios在时间转换上的差异性问题)话不多说,直接上重点. 最近接到了一个需求,很简单 ...

  2. 【转】MySQL乐观锁在分布式场景下的实践

    背景 在电商购物的场景下,当我们点击购物时,后端服务就会对相应的商品进行减库存操作.在单实例部署的情况,我们可以简单地使用JVM提供的锁机制对减库存操作进行加锁,防止多个用户同时点击购买后导致的库存不 ...

  3. Java中==与equals的区别及理解

    区别: "==" 比较的是两个引用在内存中指向的是不是同一对象(即同一内存空间),也就是说在内存空间中的存储位置是否一致. 如果两个对象的引用相同时(指向同一对象时)," ...

  4. 创建元素节点createElement

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  5. 关于flex的布局理解

    flex布局是一种新的布局方式也就是弹性盒子:在布局上更加方便,但是要注意的是在flex布局中float.position是无效的. 图片来自阮一峰的博客 .下面我们进入正题: flex布局分为分为主 ...

  6. HDU5919:Sequence II

    题面 Vjudge Sol 给一个数列,有m个询问,每次问数列[l,r]区间中所有数的第一次出现的位置的中位数是多少,强制在线 主席树 询问区间内不同的数的个数 树上二分找到那个中位数 # inclu ...

  7. c# ffmpeg视频转换【转载】

    c#  ffmpeg视频转换 什么是ffmpeg,它有什么作用呢,怎么可以使用它呢,带着问题去找答案吧!先参考百度百科把,我觉得它很强大无奇不有,为了方便大家我就把链接提供了! http://baik ...

  8. 用JavaScript写一个区块链

    几乎每个人都听说过像比特币和以太币这样的加密货币,但是只有极少数人懂得隐藏在它们背后的技术.在这篇博客中,我将会用JavaScript来创建一个简单的区块链来演示它们的内部究竟是如何工作的.我将会称之 ...

  9. 项目角度谈矢量切片运用以及Geoserver处理自定义规格矢量切片方案

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1. 背景 矢量切图方案目前已经是很常见的一个方案,在2016年时团队 ...

  10. 蓝桥杯java 关于大范围时间的

    import java.util.Calendar; import java.util.Date; public class A { /*这道题学习使用了java的calendar和类,主要是对dat ...