public List<List<Integer>> threeSum(int[] nums) {
if(nums==null||nums.length<3){
return new ArrayList<List<Integer>>();
}
Arrays.sort(nums);
return findKSumInOrderArray(nums, 0, 3, 0);
}
public List< List<Integer> > findKSumInOrderArray(int[] nums, int begin, int count, int target){
List< List<Integer> > res = new ArrayList<List<Integer>>();
HashSet<Integer> visited = new HashSet<Integer>();
if(count==2){
int f = begin, r = nums.length-1;
while(f < r){
int sum = nums[f] + nums[r];
if((sum==target) && (!visited.contains(nums[f])) ){
List<Integer> tuple = new ArrayList<Integer>();
tuple.add(nums[f]);
tuple.add(nums[r]);
visited.add(nums[f]);
visited.add(nums[r]);
res.add(tuple);
++f;
--r;
}else if(sum < target){
++f;
}else{
--r;
}
}
}else{
for(int i=begin;i<nums.length;++i){
if(!visited.contains(nums[i])){
visited.add(nums[i]);
List<List<Integer>> subset = new ArrayList<List<Integer>>();
subset = findKSumInOrderArray(nums, i+1, count-1, target-nums[i]);
if(!subset.isEmpty()){
for(int j=0;j<subset.size();++j){
List<Integer> tmp = subset.get(j);
tmp.add(0, nums[i]);
}
res.addAll(subset);
}
}
}
}
return res;
}
15 3Sum
最新推荐文章于 2016-05-08 15:21:43 发布