[lintcode medium]4 sum

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

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)

Note

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

public class Solution {
    /**
     * @param numbers : Give an array numbersbers of n integer
     * @param target : you need to find four elements that‘s sum of target
     * @return : Find all unique quadruplets in the array which gives the sum of
     *           zero.
     */
    public ArrayList<ArrayList<Integer>> fourSum(int[] numbers, int target) {
        /* your code */
        ArrayList<ArrayList<Integer>> result=new ArrayList<ArrayList<Integer>>();
        if(numbers==null || numbers.length==0) return result;
        int n=numbers.length;

        Arrays.sort(numbers);
        for(int i=0;i<n-3;i++)
        {
            if(i!=0&& numbers[i]==numbers[i-1])
            {
                continue;
            }
            for(int j=i+1;j<n-2;j++)
            {
                if(j!=i+1&& numbers[j]==numbers[j-1])
                {
                    continue;
                }

                int left=j+1;
                int right=n-1;
                while(left<right)
                {
                   int sum=numbers[i]+numbers[j]+numbers[left]+numbers[right];
                   if(sum==target)
                   {
                     ArrayList<Integer> list=new ArrayList<Integer>();
                     list.add(numbers[i]);
                     list.add(numbers[j]);
                     list.add(numbers[left]);
                     list.add(numbers[right]);
                     result.add(list);
                     left++;
                     right--;
                     while(left<right && numbers[left]==numbers[left-1])
                     {
                       left++;
                     }
                     while(left<right && numbers[right]==numbers[right+1])
                     {
                      right--;
                      }
                   }
                  else if(sum>target)
                  {
                    right--;
                  }
                  else
                  {
                    left++;
                  }

                }
            }
        }
        return result;
    }
}
时间: 2024-10-12 19:30:52

[lintcode medium]4 sum的相关文章

[lintcode medium] Two sum

Two Sum Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please no

LintCode &quot;Continuous Subarray Sum II&quot;

Flip over your mind: in rotated subarray case, we can simply cut the continuous smallest subarray. class Solution { public: /** * @param A an integer array * @return A list of integers includes the index of * the first number and the index of the las

[LintCode] Continuous Subarray Sum 连续子数组之和

Given an integer array, find a continuous subarray where the sum of numbers is the biggest. Your code should return the index of the first number and the index of the last number. (If their are duplicate answer, return anyone) Have you met this quest

[lintcode medium]Maximum Subarray II

Maximum Subarray II Given an array of integers, find two non-overlapping subarrays which have the largest sum. The number in each subarray should be contiguous. Return the largest sum. Example For given [1, 3, -1, 2, -1, 2], the two subarrays are [1,

lintcode medium Best Time to Buy and Sell Stock I,II,III

Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm

[lintcode medium]Maximum Subarray Difference

Maximum Subarray Difference Given an array with integers. Find two non-overlapping subarrays A and B, which |SUM(A) - SUM(B)| is the largest. Return the largest difference. Example For [1, 2, -3, 1], return 6. Note The subarray should contain at leas

[lintcode medium]Palindrome Linked List

Palindrome Linked List Implement a function to check if a linked list is a palindrome. Example Given 1->2->1, return true Challenge Could you do it in O(n) time and O(1) space? //// 1\find out the medium index of Linked list 2\ reverse the right par

[Leetcode 1, Medium] Two sum

Problem: Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please n

LintCode &quot;Continuous Subarray Sum&quot;

A variation to a classical DP: LCS. class Solution { public: /** * @param A an integer array * @return A list of integers includes the index of * the first number and the index of the last number */ vector<int> continuousSubarraySum(vector<int>