题目描述:

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. java课堂练习之可变參数与卫条件

    /*  有人邀请A,B,C,D,E,F 6个人參加一项会议,这6个人有些奇怪.由于他们有非常多要求,已知:  1)A,B两人至少有1人參加会议:  2)A,E,F 3人中有2人參加会议.  3)B和C ...

  2. Java Swing 探索(一)LayoutManager

    BorderLayout FlowLayout GridLayout GridBagLayout CardLayout BoxLayout 1.BorderLayout java.lang.Objec ...

  3. iOS开发——使用技术OC篇&简单九宫格锁屏功能的实现与封装

    简单九宫格锁屏功能的实现与封装 首先来看看最后的实现界面. 在这开始看下面的内容之前希望你能先大概思考活着回顾一下如果 你会怎么做,只要知道大概的思路就可以. 由于iphone5指纹解锁的实现是的这个 ...

  4. &quot;undefined reference to&quot; 问题解决方法

    近期在Linux下编程发现一个诡异的现象,就是在链接一个静态库的时候总是报错,类似以下这种错误: (.text+0x13): undefined reference to `func' 关于undef ...

  5. spring jdbctemplate源码跟踪

    闲着没事,看看源码也是一种乐趣! java操作数据库的基本步骤都是类似的: 1. 建立数据库连接 2. 创建Connection 3. 创建statement或者preparedStateement ...

  6. How to installation V145 Renault CAN Clip diagnostic software

    Eobd2.fr has launched the new 2015 V145 Renault CAN Clip diagnostic tool (SP19-A and SP19-B). Here i ...

  7. Java Map接口

    Map接口映射唯一键的值.一个关键是,要使用在日后检索值对象. 给定一个键和一个值,可以在一个Map对象存储的值.后的值被存储时,可以使用它的键检索. 抛出一个NoSuchElementExcepti ...

  8. 单线程模型中Message、Handler、Message Queue、Looper之间的关系

    1. Android进程 在了解Android线程之前得先了解一下Android的进程.当一个程序第一次启动的时候,Android会启动一个LINUX进程和一个主线程.默认的情况下,所有该程序的组件都 ...

  9. android网络请求库volley方法详解

    使用volley进行网络请求:需先将volley包导入androidstudio中 File下的Project Structrue,点加号导包 volley网络请求步骤: 1. 创建请求队列     ...

  10. SpringData JPA详解

    Spring Data JPA 1.    概述 Spring JPA通过为用户统一创建和销毁EntityManager,进行事务管理,简化JPA的配置等使用户的开发更加简便. Spring Data ...