lintCode 58. 四数之和

参考3数之和的逻辑写的:
https://github.com/Blankj/awesome-java-leetcode/blob/master/note/015/README.md
代码如下:

  public List> fourSum(int[] numbers, int target) {
        // write your code here
        Arrays.sort(numbers);
        List> list = new ArrayList<>();
        int i = 0,j=0;
        for(i=0;ilength-3;i++){
            j=i+1;
            while(jlength-2){ 
             int left = j+1,right = numbers.length-1;
             while(leftint sum =    numbers[i]+numbers[j]+numbers[left]+numbers[right];
             if(sum==target){
                 list.add(Arrays.asList(numbers[i],numbers[j],numbers[left++],numbers[right--]));
                 while(left1]){
                     ++left;
                 }
                 while(left1]){
                     --right;
                 }

             }else if(sumelse{
                 --right;
             }
          } 
          while(numbers[j]==numbers[++j]&&jlength-1);
            }
             while(ilength-1&&numbers[i]==numbers[i+1]){
                 i++;
             }
        }
        return list;
    }

你可能感兴趣的:(java,lintcode)