package ThreeSum; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * User: Danyang * Date: 1/17/2015 * Time: 13:10 * * 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) */ public class Solution { /** * Two Pointers * You need to ask a lots of questions * Notice: * 1. jump * 2. don't box num * * @param num * @return */ public List> threeSum(int[] num) { List> ret = new ArrayList<>(); Arrays.sort(num); int n = num.length; for(int i=0; i=0&&A.get(k)==A.get(k+1)) while(j0) k--; else j++; } i++; while(i