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 ...
随机推荐
- 简单入门使用GitHub总结
简单入门使用GitHub总结 ps: 试着自己弄一下GitHub,发现问题多多,中间还差点把自己的资料给全部弄没了(直接摔啦~). 然后网上查找各种资料进行设置终于成功了. 以下是参考的资料链接: G ...
- [读书笔记]xampp mysql启动失败解析(win7)
1. [mysql] MySQL Service detected with wrong path [mysql] Change XAMPP MySQL and Control Panel s ...
- FaceBook页面加载技术
1. 技术背景 FaceBook页面加载技术 试想这样一个场景,一个经常访问的网站,每次打开它的页面都要要花费6 秒:同时另外一个网站提供了相似的服务,但响应时间只需3 秒,那么你会如何选择呢?数据表 ...
- hibernate事务控制
在使用ssh中将事务委托给spring时老是出现事务不可用 经过检查,原因如下: 是因为在hibernate.cfg.xml文件中忘记进行了如下设置: hibernate.current_sessio ...
- bat脚本-set(setlocal enabledelayedexpansion) 学习
设置本地为延迟扩展.其实也就是:延迟变量,全称延迟环境变量扩展. 事件一: @echo off set a=4 set a=5&echo %a% pause 解说:为什么是4而不是5呢?在ec ...
- 移动端 web开发心得
吐槽:华为p9 内置浏览器就是个渣,各种奇葩的bug,1.如果将一个输入框的最大长度设为4,然后输入4个后,js将输入的值置空,就无法再输入了 手机上浏览器比较蛋疼的是,点击一个输入框,输入法将页面挡 ...
- Payload Inject And Fake
常见捆绑注入payload手法 Payload捆绑注入 注入exe型+编码: msfvenom -a <arch> --plateform <platform> -p < ...
- 第八章 监控 Zabbix
1.1 YUM部署 Zabbix 2.4 1. 前期准备(省略) l Yum源的配置 cd /etc/yum.repos.d/; mkdir old;mv ./* old/ rpm -Uvh ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题十二 HDU 1176 免费馅饼
题意: 中文题意不解释…… 思路: 先把x,T存到矩阵里 然后像数塔一样从最底层走一边就行了 dp[i][j]代表在时间为j时 第i个位置最多能吃到多少个馅饼 最后输出第0时刻的5位置的馅饼数量就好了 ...
- 免费DDOS攻击测试工具大合集
FreeBuf微科普: DoS(Denial Of Service)攻击是指故意的攻击网络协议实现的缺陷或直接通过野蛮手段残忍地耗尽被攻击对象的资源,目的是让目标计算机或网络无法提供正常的服务或资源访 ...