Leetcode015 3Sum
public class S015 {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> result = new ArrayList<List<Integer>>();
for(int i =0;i<nums.length;i++){
if(i>0&&nums[i]==nums[i-1]){
continue;//重复的直接跳过
}
int left = i+1;//从i+1开始也是防止重复的办法
int right = nums.length-1;
while(left<right){
if(nums[left]+nums[right] == -nums[i]){
List<Integer> temp = new ArrayList<Integer>();//必须每次新建
temp.add(nums[i]);
temp.add(nums[left]);
temp.add(nums[right]);
Collections.sort(temp);
result.add(temp);
//特别注意下面两个while循环
left++;
right--;//防止重复
while(left<right&&nums[left]==nums[left-1]){
left++;//防止重复
}
while(left<right&&nums[right]==nums[right+1]){
right--;//防止重复
}
//这两个条件特别重要,思考一下为何分别是left++和right--;
}else if(nums[left]+nums[right] < -nums[i]){
left++;
}else{
right--;
}
}
}
return result;
}
}
Leetcode015 3Sum的更多相关文章
- LeetCode: 3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 3Sum algorithm - 非常容易理解的实现 (java)
原题重述:(点击图片可以进入来源链接) 这到题目的中文解释是, 输入一个数组,例如{-1 0 1 2 -1 -4},从数组中找三个数(a,b,c),使得其和0,输出所有的(a,b,c)组合. 要求ab ...
- [LeetCode] 3Sum Smaller 三数之和较小值
Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...
- [LeetCode] 3Sum Closest 最近三数之和
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
- [LeetCode] 3Sum 三数之和
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all un ...
- 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 num ...
- LeetCode:3Sum, 3Sum Closest, 4Sum
3Sum Closest Given an array S of n integers, find three integers in S such that the sum is closest t ...
- 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 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...
随机推荐
- js导航栏样式变换
<script type="text/javascript"> $(function(){ var lis = $(".submenu").chil ...
- AndroidStudio0.5.2 BUG 导致 menu 菜单键崩溃
郁闷了半天,今天发现一点击手机 menu 键应用就崩溃了,记得之前都是好好的,调试了半天代码还是搞不定,于是网上google了一番,发现仅国外有一两篇文章有提到类似问题,据说是 0.5.2 版本的 B ...
- MyBatis学习-映射文件标签篇(select、resultMap)
MyBatis 真正的核心在映射文件中.比直接使用 JDBC 节省95%的代码.而且将 SQL 语句独立在 Java 代码之外,可以进行更为细致的 SQL 优化. 一. 映射文件的顶级元素 selec ...
- c# 强制退出程序
引用:http://blog.csdn.net/tanhua103292/article/details/4283203 1.强制退出WinForm程序之Application.Exit和Enviro ...
- layer ifram 弹出框
父层 <div class="col-xs-4 text-left" style="padding-left: 50px;"><button ...
- CNN-梯度下降
一.梯度检测: 对于函数而言通常有两种计算梯度的方式: 1.数值梯度 (numberical gradient) 2.解析梯度 (analytic gradient ) 数值梯度计算通常为: 更为常见 ...
- Unity性能优化——LOD技术
LOD,中文名多层次细节,是游戏中最常用的技术,它按照模型的位置和重要程度决定物体渲染的资源分配,降低非重要物体的面数和细节度,从而获得高效率的渲染运算.今天我们来实现使用它来做一个简单的优化例子. ...
- nyoj 523 双向广搜
题目链接: http://acm.nyist.net/JudgeOnline/problem.php?pid=523 #include<iostream> #include<cstd ...
- jqery总结
- android设备中USB转串口demo 下载
http://files.cnblogs.com/guobaPlayer/testUSB2Serial.apk USB转串口demo程序, 无需驱动,只要手机USB是OTG类型,插上我们的模块即可使用 ...