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 ...
随机推荐
- 【译】gRPC负载均衡
原文地址:https://github.com/grpc/grpc/blob/master/doc/load-balancing.md gRPC负载均衡 范围 本文档解释了gPRC的负载均衡的设计. ...
- ASP.NET MVC 中@Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction
1.Action.RenderAction加载办法的视图,履行Controller → Model → View的次序,然后把产生的页面带回到本来的View中再回传.而Partial.RenderPa ...
- EF6CodeFirst+MVC5+Autofac泛型注册 入门实例
贴一个EF6 CodeFirst模式结合MVC5和Autofac(泛型注册)的一个入门实例 网上类似的例子实在太少,最近自己也有用到这一块的知识,总结了一下,不要让后人踩了自己踩过的坑. 1:新建三个 ...
- RobotFramework下的http接口自动化Set Request Body 关键字的使用
Set Request Body关键字用来设置http 请求时的body 信息,尤其是在post 请求时,经常需要用到这个关键字. 该关键字接收一个参数,[ body ] 示例1:登录博客园(http ...
- SpringMVC 框架系列之组件概述与配置详解
在上一篇文章 SpringMVC 框架系列之初识与入门实例 的实例中,我们已经知道,SpringMVC 框架是一个 web 层的框架,本篇文章就详细解释一下 SpringMVC 框架具体文件的配置以及 ...
- 【BZOJ2337】Xor和路径(高斯消元)
[BZOJ2337]Xor和路径(高斯消元) 题面 BZOJ 题解 我应该多学点套路: 对于xor之类的位运算,要想到每一位拆开算贡献 所以,对于每一位拆开来看 好了,既然是按位来算 我们就只需要计算 ...
- [HDU2296]Ring
vjudge Description For the hope of a forever love, Steven is planning to send a ring to Jane with a ...
- SDP(12): MongoDB-Engine - Streaming
在akka-alpakka工具包里也提供了对MongoDB的stream-connector,能针对MongoDB数据库进行streaming操作.这个MongoDB-connector里包含了Mon ...
- Java并发编程实战(chapter_1)(原子性、可见性)
混混噩噩看了很多多线程的书籍,一直认为自己还不够资格去阅读这本书.有种要高登大堂的感觉,被各种网络上.朋友.同事一顿外加一顿的宣传与传颂,多多少少再自我内心中产生了一种敬畏感.2月28好开始看了之后, ...
- 12.C++-构造函数与析构函数调用顺序,const成员函数,const对象
单个对象创建时,构造函数的调用顺序 1.首先判断该对象的类是否拥有父类,若有则先调用父类的构造函数 2.判断该对象的成员是否是其它类的成员,若是则调用成员变量的构造函数(调用顺序和声明顺序相同) 3. ...