题目描述:

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. IIS配置相关

    转:让Windows Server 2008 + IIS 7+ ASP.NET 支持10万并发请求 http://www.cnblogs.com/dudu/archive/2009/11/10/160 ...

  2. ios上比较好用的Cydia插件

    1.iFile查看系统文件 2.KuaiDial归属地数据库 3.KuaiDial电话拨号助手 4.搜狗输入法 Photo Editor 房贷计算器

  3. Blob未完成(待优化)

    /**************************************************/ /* Author : Anby */ /* Using :兼容powerBuilder中的B ...

  4. android之存储篇——SQLite数据库

    转载:android之存储篇_SQLite数据库_让你彻底学会SQLite的使用 SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么. 例如:可以在In ...

  5. nopCommerce 数据缓存

    为了提高一个系统或网站的性能和IO吞吐量,我们一般都会采用缓存技术.当然NopCommerce也不例外,本文我们就来给大家分析一下nop中Cache缓存相关类设计.核心源码及实现原理. 一.Nop.C ...

  6. 实例源码--IOS高仿微信打飞机游戏(完整功能)

    下载源码 技术要点: 1. IOS游戏开发基础框架 2. 高仿打飞机游戏 3. 游戏背景音频技术 4.源码详细的中文注释 ……. 详细介绍: 1. IOS游戏开发基础框架 此套源码为涉及IOS游戏开发 ...

  7. Hibernate Validation注解列表

    下面是主要的验证注解及说明: 注解 适用的数据类型 说明 @AssertFalse Boolean, boolean 验证注解的元素值是false @AssertTrue Boolean, boole ...

  8. spring mvc使用的一些注意事项

    一天不进步,就是退步! 1.静态文件的处理 可以使用<mvc:resources mapping="/static/**" location="/WEB-INF/s ...

  9. js动态增加表格

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. Android Studio常用快捷键汇总(mac)

    查看原文:http://blog.csdn.net/u010818425/article/details/52266195 mac上按键符号 ⌥ : option / alt ⇧ : shift ⌃ ...