# Simple Java Beats 90%

• ``````public class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
if(nums.length<4) return new ArrayList<List<Integer>>();
List<List<Integer>> result=new ArrayList<List<Integer>>();
Arrays.sort(nums);
int l,r,sum;
int length=nums.length;
for(int i=0;i<length-3;i++){
if(i>0 && nums[i]==nums[i-1]) continue;
if(nums[i]+nums[i+1]+nums[i+2]+nums[i+3]>target) break;
if(nums[i]+nums[length-1]+nums[length-2]+nums[length-3]<target) continue;
for(int j=length-1;j>i+2;j--){
if(j<nums.length-1 && nums[j]==nums[j+1]) continue;
if(nums[i]+nums[j]+nums[i+1]+nums[i+2]>target) continue;
if(nums[i]+nums[j]+nums[j-1]+nums[j-2]<target) break;
l=i+1;
r=j-1;
while(l<r){
sum=nums[i]+nums[j]+nums[l]+nums[r];
if(sum==target){
while(++l<r && nums[l]==nums[l-1]);
while(--r>l && nums[r]==nums[r+1]);
}
else if(sum<target){
while(++l<r && nums[l]==nums[l-1]);
}
else{
while(--r>l && nums[r]==nums[r+1]);
}
}
}
}
return result;
}
``````

}

• it seems to me your code is over complicated. within for-loop you have another 3 layers of nested loops with several if else condition.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.