4sum

来源:互联网 发布:淘宝点击分享怎么点击 编辑:程序博客网 时间:2024/05/16 00:36
packagecom.ytx.hash;
importjava.util.ArrayList;
importjava.util.Arrays;
/** 题目: 4sum
 *
 *     描述: Given an array S of n integers, are there elements a, b, c,
 *                  and d in S such that a + b + c + d = target? Find all uniquequadruplets
 *                  in the array which gives the sum of target.
                    Note:
                    Elements in aquadruplet (a,b,c,d) must be in non-descending order. (ie, a ≤ b ≤ c ≤ d)
                    The solution set must not contain duplicatequadruplets.
                    For example, given array S = {1 0-1 0-2 2}, and target = 0.
                    A solution set is:
                    (-1,  0, 0, 1)
                    (-2,-1, 1, 2)
                    (-2,  0, 0, 2)
 * 
 *@authoryuantian xin
 *
 */
publicclass Four_Sum {
       
       ArrayList< ArrayList<Integer> >res;
       publicArrayList<ArrayList<Integer>> fourSum(int[]num,int target) {
             intlen = num.length;
             res= new ArrayList<ArrayList<Integer>>();
             if(num== null || len == 0) return res;
             Arrays.sort(num);
             inti = 0, j = 0;
             for(i= 0; i < len - 3; i++) {
                    if(i != 0 && num[i] ==num[i- 1]) {
                           continue;
                    }
                    
                    for(j= i + 1; j < len - 2; j++) {
                           if(j != i + 1 && num[j] ==num[j- 1]) {
                                 continue;
                           }
                           
                           TwoSum(num,i,j,j+1,len - 1, target);
                    }
             }
             
             returnres;
       }
       
       publicvoid TwoSum(int[]num,int i, int j,int begin, int end,int target) {
             while(begin< end) {
                    
                    intsum = num[i] +num[j] +num[begin] +num[end];
                    
                    if(sum< target) {
                           begin++;
                    }else if(sum> target) {
                           end--;
                    }else {
                           ArrayList<Integer>temp = newArrayList<Integer>();
                           temp.add(num[i]);
                           temp.add(num[j]);
                           temp.add(num[begin]);
                           temp.add(num[end]);
                           res.add(temp);
                           begin++;
                           end--;
                           while(begin< end && num[begin] ==num[begin- 1]) {
                                 begin++;
                           }
                           while(begin< end && num[end] ==num[end+ 1]) {
                                 end--;
                           }
                    }
             }
       }
       publicstatic void main(String[]args) {
             intnum[] = {1,0,-1,0,-2,2};
             ArrayList<ArrayList<Integer>>lists = newArrayList<>();
             lists= new Four_Sum().fourSum(num, 0);
             for(ArrayList<Integer>arrayi : lists) {
                    System.out.print("[");
                    for(Integeri : arrayi) {
                           System.out.print(i+ " ");
                    }
                    System.out.println("]");
             }
             
       }
}