# Easiest Java Solution

• Sort the array, iterate through the list, and use another two pointers to approach the target.

``````public List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i + 2 < nums.length; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {              // skip same result
continue;
}
int j = i + 1, k = nums.length - 1;
int target = -nums[i];
while (j < k) {
if (nums[j] + nums[k] == target) {
j++;
k--;
while (j < k && nums[j] == nums[j - 1]) j++;  // skip same result
while (j < k && nums[k] == nums[k + 1]) k--;  // skip same result
} else if (nums[j] + nums[k] > target) {
k--;
} else {
j++;
}
}
}
return res;
}``````

• Hi there, thank you for providing such clean and efficient code. I have a question regarding first line. When I use List<List<Integer>> result = new ArrayList<List<Integer>>(), TLE happens and that's the only thing I changed from your code! Do you know what happened?

• Hi, thanks for your question! I guess you are returning `List<List<Integer>>`. You can either use

``````List<List<Integer>> result = new ArrayList<List<Integer>>();
``````

or use type inference

``````List<List<Integer>> result = new ArrayList<>();
``````

Your change might not be the right syntax.

• Actually I'm using the first syntax you provided and it keeps TLE.

• Hi @dqf1992, I tried that and I was able to being accepted. Maybe you have something incorrect in the middle.

• smart code to skip duplicates

• @yanggao Thanks :)

• I have the same problem as you,but I find I don't code this sentence "j++; k--;"

• "Skip the same result " is so important...

• I am wondering about the time complexity.
Sorting the input is n log(n)
Looping on each other two elements is n^2
Should not the overall complexity be n^2 + n log(n)?

• that's very smart

• @yanggao I think so

• i dont know why first step is sort the array,is this only prevent duplicate?can someone explain to me,sorry for my english,thanks

• @asafapowellyb Yes. If you don't skip then you will need to use a set to store the triplets somehow. It works together with the while loop to skip duplicates once you find a match when nums[j]+nums[k]==target.

I wonder if anyone could explain here, why, while the elements of the triple ALWAYS 'skip' the replicate (the 'continue'), the last two elements (marked by 'low' and 'high') ONLY do the 'skipping' when we found a valid triple?

Thanks!

• @pep.o
n^2 is much bigger than nlogn when n --> infinite.
Similarly, suppose step 1 uses nlogn and step 2 uses n, the overall time cost will be nlogn.

• @pep.o O(n^2) trumps O(nlogn) so the overall complexity becomes n^2

• This post is deleted!

• ``````} else if (nums[j] + nums[k] > target) {
k--;
} else {
j++;
}
``````

Can someone please explain this part? So if you're over the target you move the right index (k) down, but if you're under the target you move the left index (j) up. I don't understand.

• @marty7044 Since the array is sorted, we are decreasing the sum by moving k downards. If the sum is too small, we need to increase the sum by moving j forwards. k and j represent the bounds of the pointers, so you can't get larger than k, and you can't get smaller than j.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.