Question

Given an array S of n integers, are there elements abc 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 解答的更多相关文章

  1. Two Sum 解答

    Question: Given an array of integers, find two numbers such that they add up to a specific target nu ...

  2. Combination Sum 解答

    Question Given a set of candidate numbers (C) and a target number (T), find all unique combinations ...

  3. Path Sum 解答

    Question Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...

  4. Minimum Size Subarray Sum 解答

    Question Given an array of n positive integers and a positive integer s, find the minimal length of ...

  5. C 2015年真题

    1.写出程序输出结果 void main() { char p[10]="abc"; char q[]="xyz"; int i,j; i=0; while(* ...

  6. 【LeetCode题意分析&解答】40. Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...

  7. Sum Root to Leaf Numbers 解答

    Question Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent ...

  8. 3 Sum Closest 解答

    Question Given an array S of n integers, find three integers in S such that the sum is closest to a ...

  9. Combination Sum II 解答

    Question Given a collection of candidate numbers (C) and a target number (T), find all unique combin ...

随机推荐

  1. Spring MVC + Spring MongoDB + Querydsl 通过maven整合实例

    效果图 一共3个页面:注册页,欢迎页,用户列表页 很简单的例子,主要是为了把流程走通,没有各种验证. 注册页: 欢迎页: 用户列表页: 源码地址 https://github.com/lemonbar ...

  2. [Redux] Extracting Presentational Components -- Todo, TodoList

    Code to be refactored: let nextTodoId = 0; class TodoApp extends Component { render() { const { todo ...

  3. 【转】Cocoa中的位与位运算

    转自:http://www.tuicool.com/articles/niEVjy 介绍 位操作是程序设计中对位模式或二进制数的一元和二元操作. 在许多古老的微处理器上, 位运算比加减运算略快, 通常 ...

  4. css伪类选择器详细解析及案例使用-----伪元素

    伪元素:(css3中将所有伪元素前变成了两个冒号,即::first-letter.::first-line.::before.::after.::selection.目的是为了区分伪元素与伪类.对于I ...

  5. transition过渡的趣玩

    本例中将三张图(来自网络)进行堆叠,鼠标悬停触发.附有源代码

  6. 模型 Model

    模型层包含所有视图或控制器不包含的应用程序逻辑 模型应该包含所有应用程序业务逻辑和数据库访问逻辑 主要部分 bll和dal 例如,使用ado.net或者ef5.0访问sql数据库的代码

  7. Sql Server根据表名获得所有列及其属性

    select a.name columnname,c.name as typename,case when a.is_nullable =0 then 'Not Null' else 'Null' e ...

  8. IE8下载按钮失效

    <input id="Button1" class="btn-lg-gary" type="button" onclick=" ...

  9. unity绘制线和绘制面

    绘制线条代码,其实就是指定至少两个点,然后赋予贴图即可,不废话,上代码: using UnityEngine; using System.Collections; public class LineT ...

  10. JDBC中PreparedStatement和Statement的区别

    共同点: PreparedStatement和Statement都是用来执行SQL查询语句的API之一. 不同点: 在PreparedStatement中,当我们经常需要反复执行一条结构相似的sql语 ...