File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ //2 pointers
3
+ public List<List<Integer>> threeSum(int[] nums) {
4
+ Arrays.sort(nums);
5
+ LinkedList<List<Integer>> sol = new LinkedList<List<Integer>>();
6
+
7
+ for (int i = 0; i < nums.length - 2; i++) {
8
+ if (i == 0 || (i > 0 && nums[i] != nums[i - 1])) {
9
+ int target = 0 - nums[i];
10
+ int left = i + 1;
11
+ int right = nums.length - 1;
12
+
13
+ while (left < right) {
14
+ if (nums[left] + nums[right] == target) {
15
+ ArrayList<Integer> miniSol = new ArrayList<>();
16
+ miniSol.add(nums[i]);
17
+ miniSol.add(nums[left]);
18
+ miniSol.add(nums[right]);
19
+ sol.add(miniSol);
20
+ while (left < right && nums[left] == nums[left + 1]) {
21
+ left++;
22
+ }
23
+ while (left < right && nums[right] == nums[right - 1]) {
24
+ right--;
25
+ }
26
+ left++;
27
+ right--;
28
+ } else if (nums[left] + nums[right] > target) {
29
+ right--;
30
+ } else {
31
+ left++;
32
+ }
33
+ }
34
+ }
35
+ }
36
+
37
+ return sol;
38
+ }
39
+ }
You can’t perform that action at this time.
0 commit comments