-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.java
More file actions
31 lines (30 loc) · 919 Bytes
/
Copy pathsolution.java
File metadata and controls
31 lines (30 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 15. 3Sum
// https://leetcode.com/problems/3sum/
// Medium | Java | Accepted 2024-09-23
// Runtime 836 ms | Memory 52.2 MB
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int target = 0;
Arrays.sort(nums);
Set<List<Integer>> s = new HashSet<>();
List<List<Integer>> output = new ArrayList<>();
for (int i = 0; i < nums.length; i++){
int j = i + 1;
int k = nums.length - 1;
while (j < k) {
int sum = nums[i] + nums[j] + nums[k];
if (sum == target) {
s.add(Arrays.asList(nums[i], nums[j], nums[k]));
j++;
k--;
} else if (sum < target) {
j++;
} else {
k--;
}
}
}
output.addAll(s);
return output;
}
}