3 Sum 解答
Question
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.
Note:
- Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
- The solution set must not contain duplicate triplets.
For example, given array S = {-1 0 1 2 -1 -4}, A solution set is: (-1, 0, 1) (-1, -1, 2)
Solution
First, we sort the array and then implement 2 pointers to get 2 Sum. Time complexity is O(n2)
Notice here, we need to consider duplicates in result.
public class Solution {
public List<List<Integer>> threeSum(int[] nums) {
if (nums == null || nums.length < 1)
return null;
Arrays.sort(nums);
int length = nums.length;
List<List<Integer>> result = new ArrayList<List<Integer>>();
for (int i = 0; i <= length - 3; i++) {
// Remove duplicates
if (i > 0 && nums[i] == nums[i - 1])
continue;
int target = 0 - nums[i];
int l = i + 1, r = length - 1;
while (l < r) {
if (nums[l] + nums[r] == target) {
result.add(Arrays.asList(nums[i], nums[l], nums[r]));
while (l < r && nums[l] == nums[l+1]) l++;
while (l < r && nums[r] == nums[r-1]) r--;
l++;
r--;
} else if (nums[l] + nums[r] < target) {
l++;
} else {
r--;
}
}
}
return result;
}
}
3 Sum 解答的更多相关文章
- Two Sum 解答
Question: Given an array of integers, find two numbers such that they add up to a specific target nu ...
- Combination Sum 解答
Question Given a set of candidate numbers (C) and a target number (T), find all unique combinations ...
- Path Sum 解答
Question Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- Minimum Size Subarray Sum 解答
Question Given an array of n positive integers and a positive integer s, find the minimal length of ...
- C 2015年真题
1.写出程序输出结果 void main() { char p[10]="abc"; char q[]="xyz"; int i,j; i=0; while(* ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- Sum Root to Leaf Numbers 解答
Question Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent ...
- 3 Sum Closest 解答
Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...
- Combination Sum II 解答
Question Given a collection of candidate numbers (C) and a target number (T), find all unique combin ...
随机推荐
- PHP 面向对象中常见关键字使用(final、static、const和instanceof)
PHP 面向对象中常见关键字的使用: 1.final :final关键字可以加在类或者类中方法之前,但是不能使用final标识成员属性. 作用: 使用final标识的类,不能被继承. 在类中使用fin ...
- nmap 使用脚本引擎进行扫描
1.下载nmap(nmap官网). 2.安装nmap. 3.编辑环境变量(windows下所需),保存.
- [原创作品]html css改变浏览器选择文字的背景和颜色
又很久没有'剥壳'了,最近在为一家公司做一个生产管理解决方案.所以都很忙.今天的话题很简单,就做一个很简单的网页特效.今天偶然浏览到一个网站,他们在选择文字时,样子不是蓝背景和白色字体那么单调,感觉这 ...
- C# WPF 建立无边框(标题栏)的登录窗口
前言:笔者最近用c#写WPF做了一个项目,此前未曾做过完整的WPF项目,算是一边学一边用,网上搜了不少资料,效率当然是不敢恭维的,有时会在一些很简单的问题上纠结很长时间,血与泪的教训可不少. 不过,正 ...
- 程序员的家!我终于拥有自己的blog了!!!
经过多次提交诚恳的家园申请,终于得到了审核通过!今天就开始了我的.net成长之路!!!
- ubuntu中安装Docker
系统要求: 必须时64位的系统,内核最低要求是3.10 查看系统内核: $ uname -r 3.11.0-15-generic 获取最新版本打Docker: $ wget -qO- https:// ...
- 《第一行代码》学习笔记4-活动Activity(2)
1.Toast是Android系统中一种好的提醒方式,程序中使用它将一些短小的信 息通知给用户,信息会在不久自动消失,不占用任何屏幕空间. 2.定义一个弹出Toast的出发点,界面有按钮,就让点击按钮 ...
- 工程与科学数值方法的Matlab实现
%stats.m function [mean,stdev]=stats(x) n=length(x);mean=sum(x)/n;stdev=sqrt(sum((x-mean).^2/(n-1))) ...
- arc engine - IName
IName对象是一个代表性对象.通过使用IName对象,可以访问它所代表的对象的一些基本属性,而不用将整个对象调入内存.我们用IWorkspace 获得一个Workspace,那可是会调入内存的,而I ...
- rsync数据同步配置
环境配置 操作系统:centos6.4_64bit A服务器IP:192.168.6.128 B服务器IP:192.168.6.129 以A服务器为基准,将A服务器文件同步到B服务器. 步骤如下: 开 ...