三数之和等于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问题的更多相关文章

  1. threesum

    算法题 问题描述:在一些给定的数中,找到三个数,他们相加的和是0,并且这三个数的组合是不能重复的 例子: input [-1, 0, -1, 2, 1] Output [[-1, 1 ,0], [-1 ...

  2. 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 ...

  3. 3Sum algorithm - 非常容易理解的实现 (java)

    原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...

  4. [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 ...

  5. Leetcode分类刷题答案&心得

    Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...

  6. 3sum问题的解决

    其实一开始想错了,把这个问题想难了,导致没有思路,现在好了很多. 题目: Given an array S of n integers, are there elements a, b, c in S ...

  7. 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 ...

  8. python leetcode 1

    开始刷 leetcode, 简单笔记下自己的答案, 目标十一结束之前搞定所有题目. 提高一个要求, 所有的答案执行效率必须要超过 90% 的 python 答题者. 1. Two Sum. class ...

  9. 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 ...

随机推荐

  1. POJ2031 Building a Space Station【最小生成树】

    题意: 就是给出三维坐标系上的一些球的球心坐标和其半径,搭建通路,使得他们能够相互连通.如果两个球有重叠的部分则算为已连通,无需再搭桥.求搭建通路的最小边长总和是多少. 思路: 先处理空间点之间的距离 ...

  2. mysql 案例~mysql元数据的sql统计

    一 简介:今天我们来收集下提取元数据的sql 二 前沿: information_schema  引擎 memory 元数据收集表 三 sql语句: 1#没有使用索引的表统计 SELECT t.TAB ...

  3. Android 中查看内存的使用情况集常用adb命令

    http://blog.csdn.net/bigconvience/article/details/35553983 http://blog.csdn.net/duantihi/article/det ...

  4. map_server地图服务器

    http://wiki.ros.org/map_server 概述 map_server提供map_server ROS节点,它提供地图数据作为一个ROS服务器.也提供map_saver命令行功能,能 ...

  5. 在Ubuntu中通过update-alternatives切换软件版本

    update-alternatives是ubuntu系统中专门维护系统命令链接符的工具,通过它可以很方便的设置系统默认使用哪个命令.哪个软件版本,比如,我们在系统中同时安装了open jdk和sun ...

  6. xpath路径前可用方法测试

    $x("string-length(//h3[@class='t'])") 8 $x("string(//h3[@class='t'])") "XPa ...

  7. 阿里云服务器搭建FTP

    操作系统:Windows Server 2008 R2企业版. 首先,创建一个用户组:ftpUsers,创建一个用户:ftpAdmin.并将ftpAdmin隶属于ftpUsers组 其次,需要安装ft ...

  8. KVM -> 虚拟机磁盘管理_03

    1.KVM磁盘管理 1.KVM qcow2.raw.vmdk等镜像格式说明:http://blog.csdn.net/zhengmx100/article/details/53887162 raw: ...

  9. 罗克韦尔 Allen-Bradley MicroLogix 1400 查看、设置IP

    =============================================== 2019/4/14_第1次修改                       ccb_warlock == ...

  10. 基于react/vue的移动端终极适配方案vw单位(更新css-modules配置)

    传送门:  https://segmentfault.com/a/1190000014185590