-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy path3_Sum.java
More file actions
44 lines (35 loc) · 1.2 KB
/
Copy path3_Sum.java
File metadata and controls
44 lines (35 loc) · 1.2 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
//Problem Link: https://leetcode.com/problems/3sum/
//Solution
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
int i, j;
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList() ;
for(int k = 0 ; k < nums.length-1 ; k++){
i =k+1;
j = nums.length-1;
int ele = nums[k];
if( k > 0 && nums[k] == nums[k-1]) continue;
while( i < j){
if( i-1 > k && nums[i] == nums[i-1] ){
i++;
continue;
}
if( -ele == nums[i] + nums[j] ){
List<Integer> list = new ArrayList();
list.add(ele);
list.add(nums[i]);
list.add(nums[j]);
res.add( new ArrayList(list) );
i++;
j--;
}
else if( -ele > nums[i] + nums[j] )
i++;
else
j--;
}
}
return res;
}
}