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. Axure使用心得分享

    因为之前很少涉及到原型设计,所以对这个原型设计工具也不太熟悉,第一次使用走了不少的弯路,在这里把自己在使用过程中的心得跟大家分享一下,希望能够对大家有所帮助. 一. 元素的选择 我觉得这是Axure原 ...

  2. ONCOCNV软件思路分析之tumor处理

    前期处理 perl脚本统计RC(RC(read counts)) 读入control baseline 和 sigma(最后baseline 预测的mad值) 将gc < 0.28或gc > ...

  3. 使用AOP的好处

    原始代码的写法 既然要通过代码来演示,那必须要有例子,这里我的例子为: 有一个接口Dao有insert.delete.update三个方法,在insert与update被调用的前后,打印调用前的毫秒数 ...

  4. 物联网框架ServerSuperIO在.NetCore实现跨平台的实践路线

    正所谓天下大势,不跟风不行.你不跨平台,很low嘛.java说:你们能跨嘛,跨给我看看.C#说:不要强人所难嘛.java说:能部署在云上吗?docker?微服务?C#说:不要强人所难嘛.java说:你 ...

  5. Python基础__函数

    本节将进入函数的介绍,函数是Python基础中最精彩的部分之一,接下来将对函数做详细介绍.函数 函数就是对代码进行一个封装.把实现某一功能的代码进行封装到一起.下次需要使用时不需要进行编写代码直接调用 ...

  6. Python Cookbook(第3版)中文版:15.18 传递已打开的文件给C扩展

    15.18 传递已打开的文件给C扩展¶ 问题¶ 你在Python中有一个打开的文件对象,但是需要将它传给要使用这个文件的C扩展. 解决方案¶ 要将一个文件转换为一个整型的文件描述符,使用 PyFile ...

  7. 【CJOJ P2110】YL杯超级篮球赛

    [CJOJ P2110]YL杯超级篮球赛 Description 一年一度的高一YL杯超级篮球赛开赛了.当然,所谓超级的意思是参赛人数可能多于5人.小三对这场篮球赛非常感兴趣,所以一场都没有落下.每天 ...

  8. BZOJ第7页养成计划

    嗯,用这篇博客当一个目录,方便自己和学弟(妹?)们查阅.不定期更新. BZOJ1600   BZOJ1601   BZOJ1602   BZOJ1603   BZOJ1604   BZOJ1605   ...

  9. Spring任务调度定时器

    1.在spring-context.xml配置 <!-- 计划任务配置,用 @Service @Lazy(false)标注类,用@Scheduled(cron = "0 0 2 * * ...

  10. mysql简单操作

    1,mysql 唤醒数据库,mysql -uroot -p11221 2,创建一个数据库: CREATE DATABASE mldn CHARACTER SET UTF8; 也可以写成小写的:crea ...