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 ...
随机推荐
- jQuery-easyui和validate表单验证实例
jQuery EasyUI 表单 - 表单验证插件validatebox 使用时需要向页面引入两个css文件如下: <link rel="stylesheet" href=& ...
- C# 简单线程实例
1.简单线程实例 以及委托(同步委托.异步委托) using System; using System.Collections.Generic; using System.Linq; using Sy ...
- wcf事务
wcf服务 using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serial ...
- javaScript之表格操作<一:新增行>
DOM表格系列操作 /** * 添加表格行 * @function 本接口可以用于:在表格tbody部分新增任意数量,任意样式的行HTML结构; * @name addTableLines * @au ...
- POJ 2503 Babelfish (STL)
题目链接 Description You have just moved from Waterloo to a big city. The people here speak an incompreh ...
- mysql 案例 ~ pt-io工具的使用
一 简介:如何使用pt-iopfile调查io具体信息二 目的:利用pt-iopfile分析mysql内部IO操作密集的文件,用以发现问题三 使用: pt-iopfile -p mysql_pid ...
- python 设计及调试的一些小技巧
在“笨办法学习python”中介绍了一些设计函数以及调试技巧: 参考网址:http://www.jb51.net/shouce/Pythonbbf/latest/ex36.html If 语句的规则¶ ...
- swift学习第一天---常量变量基础数据类型
import Foundation /** * 1.常量 变量 知识要点:常量的定义用let 变量的定义用var 常量一旦定义便不可再更改. 变量定义之后可以在定义之后的程序中任意地方进行修改. */ ...
- Python中的元类
从前面"Python对象"文章中了解到,在Python中一切都是对象,类可以创建实例对象,但是类本身也是对象. class C(object): pass c = C() prin ...
- 使用apache和nginx代理实现tomcat负载均衡及集群配置详解
实验环境: 1.nginx的代理功能 nginx proxy: eth0: 192.168.8.48 vmnet2 eth1: 192.168.10.10 tomcat server1: vmnet2 ...