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 ...
随机推荐
- 如何把Excel中的E+数值批量修改为文本格式?
日常工作中,经常会出现这样的情况,当我们把一组数据导入EXCEL表中时,本想让数字在表中全部显示出来,但是表格中却以E+的方式显示,如果数据较少,我们可以用最笨的方法一个一个的点击单元格来实现目的,但 ...
- 【BZOJ2946】公共串(后缀数组)
[BZOJ2946]公共串(后缀数组) 题面 权限题... 只有CJOJ题面啦 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: 读入单词,计算最长公共子串的 ...
- cut命令及参数企业案列讲解及awk对比
cat oldboy.txt I am oldboy myqq is 49000448 cut -d " " -f3,6 oldboy.txt oldboy 49000448 aw ...
- python PEP8相关介绍
在学习了python相关技术之后,开始重视其开发规范,以满足代码的可读性以及可维护性.主要的是学习了PEP8-style for python code的相关内容. 代码布局 缩进:每一级4个缩进.连 ...
- Redis主从配置及HA方案
首先说下主从同步Replication的原理 在Slave启动并连接到Master之后,它将主动发送一条SYNC命令.此后Master将启动后台存盘进程,同时收集所有接收到的用于修改数据集的命令,在后 ...
- fitnesse - 用例创建编辑、管理、执行和日志
fitnesse - 用例创建编辑.管理.执行和日志 2017-10-09 目录 1 用例创建编辑 1.1 用例创建 1.2 用例编辑2 用例管理3 用例测试执行和日志 3.1 用例测试执行 ...
- react-native简单demo:实现加载豆瓣电影列表
https://github.com/axel10/react-native-douban' 相关随笔: react-native 开发环境搭建 http://www.cnblogs.com/axel ...
- Python解析Yahoo的XML格式的天气预报数据
以下是Yahoo天气预报接口xml格式数据: <rss xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xm ...
- Could not get dialect instance.
一般此错误都是和数据库有关,请确认数据库配置文件是否配置正确,或者确认数据库是否连接正常
- ubuntu下cmake 使用clang
安装llvm.clang sudo apt-get install llvm clang clang命令会在/usr/bin/clang cmake配置交叉编译链 建立linux.toolchain. ...