MCPcopy Create free account
hub / github.com/DestinationFAANG/Destination-FAANG-Java-Solution / Solution

Class Solution

15 3sum/15 3-Sum.java:5–42  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3// Leetcode Link: https://leetcode.com/problems/3sum/
4
5class Solution {
6 public List<List<Integer>> threeSum(int[] nums) {
7
8 Arrays.sort(nums);
9 List<List<Integer>> result = new ArrayList<>();
10
11 for (int i = 0; i<nums.length && nums[i] <=0; i++){
12 if (i == 0 || nums[i] != nums[i-1]){
13 twoSum2(nums, i, result);
14 }
15 }
16
17 return result;
18
19 }
20
21 void twoSum2(int[] nums, int i, List<List<Integer>> result){
22 int left = i+1;
23 int right = nums.length - 1;
24
25 while(left < right){
26 int sum = nums[i] + nums[left] + nums[right];
27
28 if(sum < 0){
29 ++left;
30 }
31 else if (sum > 0){
32 --right;
33 }
34 else{
35 result.add(Arrays.asList(nums[i], nums[left++], nums[right--]));
36 while(left < right && nums[left] == nums[left-1]){
37 ++left;
38 }
39 }
40 }
41 }
42}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected