threeSum问题
三数之和等于0的问题:
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4], 满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]
思想:
由于水平问题,本人目前只能想到用一个一个遍历来找满足条件的元素。
1.首先我们定义两个集合,一个是List找<Integer>类型集合list。
2.找到满足元素后,定义一个是Integer类型的集合l,我们需要把满足条件的元素加入集合l里面。
3.然后我们判断集合list包含的集合l是否包含满足条件的元素,如果满足,说明前面我们已经加入了,可能是顺序不同,但是数字完全相同。这个时候直接移除该集合l然后跳出判断语句。
4.接下来判断集合list是否包含集合l,不包含就add
5.最后我们返回list集合,整体思路大概就是这样。
下面我们附上代码
import java.util.ArrayList;
import java.util.List; public class threeSum {
public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> list = new ArrayList<List<Integer>>();
for(int i = 0;i<nums.length-2;i++) {
for(int j = i+1;j<nums.length - 1;j++) {
for(int k = j+1;k<nums.length;k++) {
if(nums[i]+nums[j]+nums[k] == 0) {
List<Integer> l = new ArrayList<Integer>();
l.add(nums[i]);
l.add(nums[j]);
l.add(nums[k]);
for(int m = 0;m<list.size();m++) {
if(list.get(m).contains(nums[i])&&list.get(m).contains(nums[j])&&list.get(m).contains(nums[k])&&(nums[i]!=0||nums[j]!=0||nums[k]!=0)) {
list.remove(m);
break;
}
}
if(!list.contains(l)) {
list.add(l);
}
}
}
}
}
return list;
}
总结:写这段代码遇到最大的问题就是处理三个元素都是0时,条件判断语句没想好,导致写的代码看起来不美观,并且,时间复杂度也挺大的。这个代码后期会优化的。
坚持撸下去,终有一天我们也能站在最高处。 -------送给和我一样的初学者
threeSum问题的更多相关文章
- threesum
算法题 问题描述:在一些给定的数中,找到三个数,他们相加的和是0,并且这三个数的组合是不能重复的 例子: input [-1, 0, -1, 2, 1] Output [[-1, 1 ,0], [-1 ...
- LeetCode: 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 ...
- 3Sum algorithm - 非常容易理解的实现 (java)
原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...
- [LeetCode] 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分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...
- 3sum问题的解决
其实一开始想错了,把这个问题想难了,导致没有思路,现在好了很多. 题目: Given an array S of n integers, are there elements a, b, c in S ...
- 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 ...
- python leetcode 1
开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目. 提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者. 1. Two Sum. class ...
- LeetCode:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
随机推荐
- 第14月第23天 uitextfield文字下移
1. http://www.jianshu.com/p/641a0cbcabb0
- mysql 原理 ~ innodb恢复机制
举例说明 机制 数据页A的lsn为100,数据页B的lsn为200,checkpoint lsn为150,系统lsn为300,表示当前系统已经更新到300,小于150的数据页已经被刷到磁盘上,因此数据 ...
- 基于theano的降噪自动编码器(Denoising Autoencoders--DA)
1.自动编码器 自动编码器首先通过下面的映射,把输入 $x\in[0,1]^{d}$映射到一个隐层 $y\in[0,1]^{d^{'}}$(编码器): $y=s(Wx+b)$ 其中 $s$ 是非线性的 ...
- Python学习之not,and,or篇
Python学习之not,and,or篇 运算符示意 not –表示取反运算. and –表示取与运算. or –表示取或运算. 运算符优先级 not > and > or. 举例如下: ...
- C语言函数调用栈(三)
6 调用栈实例分析 本节通过代码实例分析函数调用过程中栈帧的布局.形成和消亡. 6.1 栈帧的布局 示例代码如下: //StackReg.c #include <stdio.h> //获取 ...
- vmware添加磁盘后linux无需重启识别的方法
cd /sys/class/scsi_host/ [root@centos4 scsi_host]# ls host0 host1 host2 有几个host就刷几次 [root@centos4 sc ...
- SharePoint 2013 文档库“样式”变了
有朋友反馈说文档库的样式变了. 经查证,原来有人修改了视图的"样式":库设置—视图—样式,改为默认即可. 另外,如果编辑页面,编辑web部件的属性,在"杂项"勾 ...
- mysql数据库报错:InnoDB: Operating system error number 13 in a file operation
环境:centos6.5 x86_64 启动mysql发现日志报错(日志路径可以查看/etc/my.cnf的配置) 160722 10:34:08 [Note] Found 42570716 of 4 ...
- appium运行报错java.net.SocketException: socket write error
这个错我调了 快两天一点头绪没有,脚本正常跑没问题,但是就是控制台输出信息报错,没法定位问题在哪.报错如图: 虽然这个报错不影响测试结果,但是本人有强迫症,一定要查出究竟: 我的尝试: 1.那天试验, ...
- 如何从现有版本升级到element UI2.0?使用npm-check-updates
转:https://blog.csdn.net/wojiaomaxiaoqi/article/details/78428738 登录element UI官网时提示2.0已经正式发布了,Element ...