【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 ...
随机推荐
- swift app中展示折线图, 饼状图, 柱状图等数据图表
github 下载Charts-master SDK,该SDK有多种可自定义的图表样式 lineChart 正弦余弦线图 LineChart (cubic lines) LineChart (grad ...
- 四元数(Quaternion)和旋转
四元数介绍 旋转,应该是三种坐标变换——缩放.旋转和平移,中最复杂的一种了.大家应该都听过,有一种旋转的表示方法叫四元数.按照我们的习惯,我们更加熟悉的是另外两种旋转的表示方法——矩阵旋转和欧拉旋转. ...
- CentOS6.3配置yum源
转载:http://www.linuxidc.com/Linux/2012-10/72750.htm 全新以最小化包安装了64位的CentOS6.3系统,作为本地的Web服务器使用,现记录全过程第二步 ...
- java 数组初始化
一维数组 1) int[] a; //声明,没有初始化 2) int[] a=new int[5]; //初始化为默认值,int型为0 3) int[] a={1,2,3,4,5} ...
- js的Touch事件
js的touch事件,一般用于移动端的触屏滑动$(function(){document.addEventListener("touchmove", _touch, false); ...
- eclipse的scala环境搭建
两种方法使eclipse安装scala环境(eclipse luna) 1.下载eclipse for scala IDE http://scala-ide.org/download/sdk.html ...
- 短小实用 渗透用的Python小脚本
渗透的很多时候,找到的工具并不适用,自己码代码才是王道,下面三个程序都是渗透时在网络上找不到合适工具,自己辛苦开发的,短小实用. 一.记录root密码小工具 root.py #!/usr/bin/py ...
- B - Fill
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice UV ...
- ACMer
Problem Description There are at least P% and at most Q% students of HDU are ACMers, now I want to k ...
- 【Android Studio使用教程4】Android Studio下载
鉴于Android官网上下载很慢,Android Studio等已在网盘分享:Android Studio 网盘下载路径: windows:http://yunpan.cn/cfTszP2wrJxdD ...