public class Solution {
public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
Set<ArrayList<Integer>> res = new HashSet<ArrayList<Integer>> ();
Arrays.sort(num);
for (int i=0; i<num.length-3; i++) {
for (int j=i+1; j<num.length-2; j++) {
int m = j+1;
int n = num.length-1;
while (m<n) {
if (num[i]+num[j]+num[m]+num[n]==target) {
ArrayList<Integer> temp = new ArrayList<Integer> ();
temp.add(num[i]);
temp.add(num[j]);
temp.add(num[m]);
temp.add(num[n]);
res.add(temp);
m++;
n--;
} else if (num[i]+num[j]+num[m]+num[n]<target) m++;
else n--;
}
}
}
return new ArrayList<ArrayList<Integer>> (res);
}
}
没有评论:
发表评论