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 ...
随机推荐
- 【剑指offer】面试题41:和为 s 的两个数字 VS 和为 s 的连续正数序列
题目: 输出所有和为S的连续正数序列.序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序 思路: small代表序列最小数字,large代表序列最大数字.初始化small为1,large为2. ...
- centos Minicom通信终端
minicom是linux下的串口通信软件,他使用完全使用键盘操作.它虽然没有windows下的超级终端好用,但是它也是一种串口通信的方法.一.minicom安装在超级终端中输入:sally@sall ...
- NUnit - 使用感受
Nunit使用 最近项目开始大量使用Nunit, 发现Nunit还是有很多好处的. 1. 测试驱动逻辑,这样可以尽最大可能减少“修改”引入的Bug. 如果你修改了一些东西,导致Case跑不过.请检查你 ...
- hdu 3722
单词间形成环,求最大值,,KM,,,,, #include<stdio.h> #include<string.h> #define N 210 #define inf 0x3f ...
- Redis安全
安全 执行在可信环境 Redis的安全设计是在"Redis执行在可信环境"这个前提下做出的.在生产环境执行时不能同意外界直接连接到Redisserver上.而应该通过应用程序进行中 ...
- Android 编程下 Activity 的创建和应用退出时的销毁
为了确保对应用中 Activity 的创建和销毁状态进行控制,所以就需要一个全局的变量来记录和销毁这些 Activity.这里的大概思路是写一个类继承 Application,并使获取该 Applic ...
- 验证docker的Redis镜像也存在未授权访问漏洞
看到了这篇老外的博客:Over 30% of Official Images in Docker Hub Contain High Priority Security Vulnerabilities于 ...
- oracle 随Linux系统启动自启动设置
本文转自http://www.jb51.net/article/19823.htm 首先,要在RHEL中设置允许ORACLE系统自动启动,因为默认情况下是设置为不允许的.操作如下: 在root账户下修 ...
- unity音频组件
unity 支持的四种音频格式: .AIFF 适用于较短的音乐文件可用作游戏打斗音效 .WAV 适用于较短的音乐文件可用作游戏打斗音效 .MP3 适用于较长的音乐文件可用作游戏背景音乐 .OGG ...
- ul li a active jquery.cookie.js
div class="righter nav-navicon" id="admin-nav"> <div class="mainer&qu ...