-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18.java
31 lines (31 loc) · 1.25 KB
/
18.java
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
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> result = new ArrayList<List<Integer>>();
int len = nums.length;
Arrays.sort(nums);
int sum,m,n,temp;
for(int i=0;i<len-3;i++){
if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target) break;
if(nums[i]+nums[len-1]+nums[len-2]+nums[len-3]<target) continue;
if(i>0&&nums[i]==nums[i-1]) continue;
for(int j=i+1;j<len-2;j++){
if(nums[i]+nums[j]+nums[j+1]+nums[j+2]>target) break;
if(nums[i]+nums[j]+nums[len-1]+nums[len-2]<target) continue;
if(j>i+1&&nums[j]==nums[j-1]) continue;
temp = nums[i]+nums[j];
m = j+1;n=len-1;
while(m<n){
sum = temp+nums[m]+nums[n];
if(sum==target){
result.add(Arrays.asList(nums[i],nums[j],nums[m],nums[n]));
while(m<n&&nums[m+1]==nums[m])m++;
while(m<n&&nums[n-1]==nums[n])n--;
m++;n--;
}else if(sum<target) m++;
else n--;
}
}
}
return result;
}
}