LeetCode – 3Sum
Problem:
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)
Java Solution
This problem can be solved by using two pointers. Time complexity is O(n^2).
To avoid duplicate, we can take advantage of sorted arrays, i.e., move pointers by >1 to use same element only once.
public List<List<Integer>> threeSum(int[] nums) { Arrays.sort(nums); ArrayList<List<Integer>> result = new ArrayList<>(); for (int i = 0; i < nums.length; i++) { int j = i + 1; int k = nums.length - 1; if (i > 0 && nums[i] == nums[i - 1]) { continue; } while (j < k) { if (k < nums.length - 1 && nums[k] == nums[k + 1]) { k--; continue; } if (nums[i] + nums[j] + nums[k] > 0) { k--; } else if (nums[i] + nums[j] + nums[k] < 0) { j++; } else { ArrayList<Integer> l = new ArrayList<>(); l.add(nums[i]); l.add(nums[j]); l.add(nums[k]); result.add(l); j++; k--; } } } return result; } |
<pre><code> String foo = "bar"; </code></pre>
-
Hey
-
Jekin Sanghavi
-
Rohit Kadyan
-
ryanlr
-
Mr KK
-
Lasha K
-
Cherry Zhao
-
Gurpreet singh
-
CP
-
boyhou
-
Gireesh
-
Chetan
-
Ankit
-
AmourDeMai
-
Candis
-
Omar Edgardo Lugo Sánchez
-
Mohanasundaram Veeramuthu
-
Logic Luo
-
Malay Shah
-
Adil qureshi
-
ryanlr
-
ryanlr
-
PK
-
Ranjit
-
Ranjit
-
Gaurav Sharma
-
Kick Buttowski
-
Anirudh
-
Ajay
-
Ajay
-
Yancey
-
H Chen
-
doris717
-
Serj Sintsov