题目描述:

Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

解题分析:

这道题注意一下几点即可:

1,先固定前两个数的位置,移动第三个数,这样的查找方式不会有数字组合的遗漏;

2,要考虑到数组元素有重复的情况下的处理。

3,若先为数组排序(这类题一般都这么做),要充分利用有序的特点来减少比较情况,优化代码。

具体代码:

 public class Solution {
public static List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> results = new ArrayList<List<Integer>>();
//边界情况的判断
if(nums.length<3){
//results.add(new ArrayList<Integer>());
return results;
}
if(nums.length==3){
if(nums[0]+nums[1]+nums[2]==0){
List<Integer> array =new ArrayList<Integer>();
array.add(nums[0]);
array.add(nums[1]);
array.add(nums[2]);
results.add(array);
return results;
}
else{
//results.add(new ArrayList<Integer>());
return results;
}
}
//先为数组排序
Arrays.sort(nums);
//先把前两个数确定,变第三个数得值,以保证查找了所有例子
for(int i=0;i<nums.length-2;i++){
//如果第一个数已经大于零,就没有必要再找下去了
if(nums[i]>0)
break;
for(int j=i+1;j<nums.length-1;j++){
//同上
if(nums[i]+nums[j]>0)
break;
for(int index=j+1;index<nums.length;index++){
if(nums[i]+nums[j]+nums[index]==0){
List<Integer> array = new ArrayList<Integer>();
array.add(nums[i]);
array.add(nums[j]);
array.add(nums[index]);
results.add(array);
//避免结果重复的处理
while(index+1<nums.length && nums[index+1]==nums[index]){
index++;
}
} }
//避免结果重复的处理
while(j+1<nums.length-1 && nums[j+1]==nums[j]){
j++;
} }
//避免结果重复的处理
while(i+1<nums.length-2 && nums[i+1]==nums[i]){
i++;
}
}
return results; }
}

【leetcode】15. 3Sum的更多相关文章

  1. 【LeetCode】15. 3Sum 三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...

  2. 【LeetCode】15. 3Sum 三个数和为0

    题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

  3. 【LeetCode】15、三数之和为0

    题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...

  4. 【LeetCode】16. 3Sum Closest 最接近的三数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...

  5. 【一天一道LeetCode】#15 3Sum

    一天一道LeetCode系列 (一)题目 Given an array S of n integers, are there elements a, b, c in S such that a + b ...

  6. 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...

  7. 【LeetCode】259 3Sum Smaller

    题目: Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 ...

  8. 【LeetCode】16. 3Sum Closest

    题目: Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  9. 【LeetCode】015 3Sum

    题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find al ...

随机推荐

  1. jquery easyui from 表单返回乱码!

    如果用easyui的form进行提交,必须在<form>标签中加入属性method="post",即<form method="post"&g ...

  2. 剑指 offer set 5 二进制中 1 的个数

    总结 1. 负数右移会保持其符号. 比如 0x80000000 右移, 其对应的绝对值也是 0X80000000, 其右移一位并保持符号, 得到 0XC0000000. 符号位保持, 使得负数永远都无 ...

  3. [原创]-IIS7.5优化,支持同时10万个请求

    背景:        IIS7.5是微软推出的最新平台IIS,性能也较以前有很大的提升,但是默认的设置配不适合很大的请求.但是我们可以根据实际的需要进行IIS调整,使其性能更佳,支持同时10万个请求. ...

  4. 疑难杂症:NoSuchMethodError: com.opensymphony.xwork2.util.finder.UrlSet.includeClassesUrl(Lcom/opensymphony/xwork2/util/finder/ClassLoaderInterface;)

    严重: Exception starting filter struts2java.lang.NoSuchMethodError: com.opensymphony.xwork2.util.finde ...

  5. 创建MS Office 和 WPS 兼容插件

    在工作中我们要实现一个功能,需要创建MS Office 和 WPS 兼容插件,也就是创建一个DLL,可以同时兼容office和wps.这样带来的好处就是只需要维护同一份代码,大大降低维护的工作! 1. ...

  6. redis的实现过程

    1下载redis的安装包并按照操作安装 2开启 右击我的电脑→管理→服务→站到redis service服务 将其开启 注意:redis服务开启后其默认的ip和端口号为127.0.0.1:6379 3 ...

  7. git的.gitignore配置

    .gitignore 配置文件用于配置不需要加入版本管理的文件,配置好该文件可以为我们的版本管理带来很大的便利,以下是个人对于配置 .gitignore 的一些心得. 1.配置语法: 以斜杠“/”开头 ...

  8. C++ Traits技术

    要想深入的理解STL的迭代器.分配器等,就必须了解C++模板编程中的一个技巧——Traits. 1.问题的提出 C++的模板特性为泛型编程提供了支持.这样我们就可以编写更加通用的代码,而不必过分去关心 ...

  9. Git_Windows 系统下Git安装图解

    Windows 系统下Git安装图解  心得  :很成功~ 简单来说Git是一个免费的.开源的版本控制软件,从功能上讲,跟我们比较熟悉的Subversion(SVN)这类版本控制软件没什么两样.由于工 ...

  10. Java SE ---类,方法,对象等

    1,面向对象程序设计的三大基本特征:继承(Inheritence).封装(Encapsulation).多态(Polymorphism)     2,如何定义类?            修饰符 cla ...