码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode]4Sum

时间:2014-09-13 13:25:25      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:des   style   color   io   os   java   ar   strong   for   

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 unique quadruplets in the array which gives the sum of target.

Note:

  • Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie, abcd)
  • The solution set must not contain duplicate quadruplets.

    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)

答案

public class Solution {
      public void swap(int[] array, int i, int j)
    {
        int t = array[i];
        array[i] = array[j];
        array[j] = t;
    }

    public void quickSort(int[] array, int start, int end)
    {
        if (start >= end)
        {
            return;
        }
        int middle = (start + end) / 2;
        swap(array, start, middle);
        int i = start + 1;
        while (i <= end)
        {
            if (array[i] >= array[start])
            {
                break;
            }
            i++ ;
        }
        int j = i + 1;
        for (; j <= end;)
        {
            while (j <= end)
            {
                if (array[j] < array[start])
                {
                    break;
                }
                j++ ;
            }
            if (j <= end)
            {
                swap(array, i, j);
                i++ ;
                j++ ;
            }
        }
        i-- ;
        if (array[i] < array[start])
        {
            swap(array, start, i);
        }
        quickSort(array, start, i - 1);
        quickSort(array, i + 1, end);
    }

    List<List<Integer>> twoSum(int[] array, int startIndex, int endIndex, int dest)
    {
        List<List<Integer>> result = new LinkedList<List<Integer>>();
        while (endIndex > startIndex)
        {
            int sum = array[startIndex] + array[endIndex];
            if (sum == dest)
            {
                List<Integer> p = new LinkedList<Integer>();
                p.add(array[startIndex]);
                p.add(array[endIndex]);
                result.add(p);
                for (startIndex++ ; startIndex <= endIndex; startIndex++ )
                {
                    if (array[startIndex] != array[startIndex - 1])
                    {
                        break;
                    }
                }
            }
            if (sum > dest)
            {
                endIndex-- ;
            }
            if (sum < dest)
            {
                startIndex++ ;
            }
        }
        return result;
    }

    public List<List<Integer>> fourSum(int[] num, int target)
    {
        List<List<Integer>> result = new LinkedList<List<Integer>>();
        if (num == null || num.length < 4)
        {
            return result;
        }
        int[] array = new int[num.length];
        for (int i = 0; i < num.length; i++ )
        {
            array[i] = num[i];
        }
        quickSort(array, 0, array.length - 1);
        int start = 0;
        int end = array.length - 1;
        for (start = 0; start < array.length - 3;)
        {
            for (end = array.length - 1; end - start >= 2;)
            {
                List<List<Integer>> list = twoSum(array, start + 1, end - 1, target - array[start]
                                                                             - array[end]);
                for (List<Integer> p : list)
                {
                    p.add(0, array[start]);
                    p.add(array[end]);
                    result.add(p);
                }
                for(end--;start<end;end--){
                    if(array[end]!=array[end+1])
                        break;
                } 
            }
            for (start++ ; start < array.length; start++ )
            {
                if (array[start] != array[start - 1]) break;
            }

        }
        return result;
    }
}


[leetcode]4Sum

标签:des   style   color   io   os   java   ar   strong   for   

原文地址:http://blog.csdn.net/jiewuyou/article/details/39250745

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!