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 ...
随机推荐
- SUSE linux升级perl及openssl
一.perl安装: 1.下载并解压软件:tar zxvf perl-5.24.0.tar.gz 2.运行./configure.gnu -help查看帮助,运行./configure.gnu -des ...
- Tiny6410之重定位代码到SRAM+4096
重定位代码 两个不同的地址概念: 对于程序而言,需要理解两个地址,一个是程序当前所处的地址,即程序运行时所处的当前地址.二是程序应该位于的运行地址,即编译程序时所指定的程序的链接地址.在Tiny641 ...
- Linux scp命令
语法 scp [-1246BCpqrv] [-c cipher] [-F ssh_config] [-i identity_file] [-l limit] [-o ssh_option] [-P p ...
- PHP学习过程_Symfony_(4)_命令创建实体_以及实体关系
//项目运行php app/console server:run//创建实体php app/console doctrine:generate:entitybundle名称:实体名称例如:Symfon ...
- [DP优化方法之斜率DP]
什么是斜率dp呢 大概就把一些单调的分组问题 从O(N^2)降到O(N) 具体的话我就不多说了 看论文: http://www.cnblogs.com/ka200812/archive/2012/08 ...
- NodeJs md5 sha1加密
var crypto = require('crypto');module.exports = { md5: (str)=> { return crypto.createHash('md ...
- <验证码的产生>C语言---验证码的产生和验证
无论在网页还是软件上登录时候都会遇到验证码的问题,不知道不懂其中奥秘的码友有没有兴趣一起来探讨一下. 其实并没有什么奥秘可言,就是产生随机数,然后让产生的随机数做为字符库(提前做好的数字字母字符串)的 ...
- matlab读xls数据
[ndata,label,abalone]=xlsread('data.xls') ndata:表示数字属性 label:表示类别属性 abalone:全部数据
- List<T>集合导出csv方法参考,通过增加自定义的属性控制输出的字段。
public string CreateAdvExcel(List<GridScoreManager> lt) { StringBuilder builder = new StringBu ...
- x264 亮度信号8x8帧内预测模式
1 该模式的8个预测方向与4x4帧内预测模式一样. 2 该模式只有High profile及更高的Profile的才有可能使用,Baseline.Main Profile.Preset为ultrafa ...