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 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的更多相关文章
- LeetCode 15 3Sum [sort] <c++>
LeetCode 15 3Sum [sort] <c++> 给出一个一维数组,找出其中所有和为零的三元组(元素集相同的视作同一个三元组)的集合. C++ 先自己写了一发,虽然过了,但跑了3 ...
- leetcode 15. 3Sum 二维vector
传送门 15. 3Sum My Submissions Question Total Accepted: 108534 Total Submissions: 584814 Difficulty: Me ...
- [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 ...
- LeetCode——15. 3Sum
一.题目链接:https://leetcode.com/problems/3sum/ 二.题目大意: 3和问题是一个比较经典的问题,它可以看做是由2和问题(见http://www.cnblogs.co ...
- LeetCode 15 3Sum(3个数求和为0的组合)
题目链接 https://leetcode.com/problems/3sum/?tab=Description Problem: 给定整数集合,找到所有满足a+b+c=0的元素组合,要求该组合不 ...
- 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 ...
- LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum
n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数 重复数处理: 使i为左至右第一个不重复数:while ...
- 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 ...
- leetcode 15 3sum & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
- 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 ...
随机推荐
- pat1011-1020
一开始几道题写到吐血,真的自己现在好弱 1011 水题不说了 #include<bits/stdc++.h> using namespace std; const int N = 105; ...
- python爬取youtube视频 多线程 非中文自动翻译
声明:我写的所有文章都是发在博客园的,我看到其他复制粘贴过去的 连个出处也不写,直接打上自己的水印...真是没的说了. 前言:前段时间搞了一些爬视频的项目,代码都写好了,这里写文章那就在来重新分析一遍 ...
- JVM 调优系列之图解垃圾回收
摘要: jvm必知系列,总结一些常见jvm回收机制,方便查阅 对于调优之前,我们必须要了解其运行原理,java 的垃圾收集Garbage Collection 通常被称为"GC", ...
- ThreadPoolExecutor源码分析
ThreadPoolExecutor是Java自带线程池FixedThreadPool(固定大小). SingleThreadExecutor(单线程).CacheThreadPool (无限大)的具 ...
- 新的一年新的变化!IT的大变天
今天是一个特别的日子,祝女神朋友们,节日快乐,早点下班! 新的一年,大家又忙碌在加班加点的堆代码中,bug的陪伴使我快乐使我忧伤,想想想,也奋斗了五六百的岁月,实习期向往大城市的公司,梦想着有一天与自 ...
- [cogs2701]动态树
题面戳我 sol 比较裸啊. 注意操作顺序就行了. code #include<cstdio> #include<algorithm> using namespace std; ...
- iOS开发——下载器的功能基本实现
今天,做了一个下载器的Demo,即从本地配置的Apache服务器上,下载指定的文件.这次,我们下载服务器根目录下的html.mp4文件. 按照惯例,我们先创建一个URL对象和请求. NSURL *ur ...
- MySQ备份常见问题
1.备份的时候出现2002报错,找不到/tmp/mysql.sock,这个文件 [root@centos199 backup]# mysqldump -uroot -ppassword cz-offi ...
- ES6学习总结二(数组的四个方法,字符串)
数组 1 map 映射 一个对一个 如:分数数组[34,56,78,99]映射为[不及格,不及格,及格,及格]; 等级数组[23,56,89]映射为 [ {name:'lmx',level:1,rol ...
- Django---视图
全过程:用户填写相关数据,提交相关请求,链接到对应的视图上,在此视图上(有用户传过来的数据[就是视图要处理的数据],在视图里面对数据进行业务处理,在数据库中crub数据,然后把对应的界面和界面显示需要 ...