【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 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的更多相关文章
- 【LeetCode】15. 3Sum 三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, 三数之和,题解,leetcode, 力扣,P ...
- 【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 ...
- 【LeetCode】15、三数之和为0
题目等级:3Sum(Medium) 题目描述: Given an array nums of n integers, are there elements a, b, c in nums such t ...
- 【LeetCode】16. 3Sum Closest 最接近的三数之和
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:3sum, three sum, 三数之和,题解,lee ...
- 【一天一道LeetCode】#15 3Sum
一天一道LeetCode系列 (一)题目 Given an array S of n integers, are there elements a, b, c in S such that a + b ...
- 【LeetCode】923. 3Sum With Multiplicity 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/3sum-wit ...
- 【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 ...
- 【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 ...
- 【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 ...
随机推荐
- JRebel_修改class后无法正确调试问题解决【2014-03-12】
原文地址:http://www.cnblogs.com/hbbbs/p/3596179.html 现象 修改了class,编译后,JRebel自动装载到Web容器中.但此时调试这个类会发现无法正常调试 ...
- 解决ajax.net 1.0中文乱码问题!
在使用ajax.net的UpdatePanel的时候,当requestEncoding编码为GB2312的时候,出现乱码.如果要解决这个问题最简单的就是改用utf-8了,但是原来使用GB2312, ...
- SQL Server 的事务和锁(二)-Range S-S锁
在这篇随笔中,我们的主要关注点在 Key-Range Lock.Key-Range Lock有 S-S.S-U.I-N.X-X几种情况.我们一个一个来说,力求明白.遗憾的是,这里可能会比较冗长,那么死 ...
- java解析excel2003和excel2007:The supplied data appears to be in the office 2007+XML Polonly supports OLE2 office documents
上传excel解析存到数据库时报: org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears ...
- 数据结构【二】:简单阻塞队列BlockingQueue
在POSIX多线程[一]:简单队列simple queue的基础上使用内部互斥锁和条件变量来控制并发以达到线程安全的目的,其主要用于 [生产者-消费者] 队列. 1.BlockingQueue初始化时 ...
- [经典算法] 字符串搜索Boyer-Moore
题目说明: 今日的一些高阶程式语言对于字串的处理支援越来越强大(例如Java.C#.Perl等),不过字串搜寻本身仍是个值得探讨的课题,在这边以Boyer- Moore法来说明如何进行字串说明,这个方 ...
- 【Android 界面效果33】二级listview列表
今天来实现以下大众点评客户端的横向listview二级列表,先看一下样式. 这种横向的listview二级列表在手机软件上还不太常见,但是使用过平板的都应该知道,在平板上市比较常见的.可能是因为平板屏 ...
- iOS 虚拟机测试出现的相关问题
一.报红 1.问题描述:自己在工程目录下新建文件夹 包其他文件夹下的.h 和.m文件拖到该文件夹 再删除工程中的报红文件复制进去新文件夹 运行报错:No such file or directory: ...
- ValidateRequest="false" 无效
在做牛腩新闻发布系统的时候,部分同学可能会遇到这样的情况: 从客户端(ContentPlaceHolder1_m_ContentPlaceHolder_ftbContent="<P&g ...
- input输入框,回车激活按钮事件代码
<input type="text" name="输入框ID" id="输入框ID" onkeypress="if(even ...