Leetcode-18 4Sum

Source: Internet
Author: User

Leetcode-18 4Sum
Zookeeper

Problem description: Givenan arraySOfNIntegers, are there elementsA,B,C, AndDInSSuch thatA+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 containduplicate quadruplets.

    For example, given array S = {1 0-1 0-22}, and target = 0.

    A solution set is:

    (-1, 0, 0, 1)

    (-2,-1, 1, 2)

    (-2, 0, 0, 2)

    Problem Analysis: The KSum problem is eventually converted to the basic 2Sum problem. At the same time, eliminate duplicate values.

    Code:

    Java solution:

     

    Public class Solution {public List
       
        
    > FourSum (int [] num, int target) {List
        
         
    > Result = new ArrayList
         
          
    > (); If (num = null | num. length <= 3) return result; // sorts the array first. Arrays. sort (num); int temp_target = 0; // the target value for every 2Sum conversion (int I = 0; I <num. length-3; I ++) {// eliminate the repeated value while (I! = 0) & (I <num. length-3) & (num [I] = num [I-1]) + + I; // you must pay attention to the array boundary judgment after deduplication, avoid if (I <num. length-3) {for (int j = I + 1; j <num. length-2; j ++) {while (j! = I + 1) & (j <num. length-2) & (num [j] = num [j-1]) + + j; if (j <num. length-2) {temp_target = target-num [I]-num [j]; int start = j + 1; int end = num. length-1; while (start <end) {int temp_sum = num [start] + num [end]; if (temp_sum <temp_target) {++ start ;} else if (temp_sum> temp_target) {-- end;} else {List
          
           
    List = new ArrayList <> (); list. add (num [I]); list. add (num [j]); list. add (num [start ++]); list. add (num [end --]); result. add (list); // continue the search and remove the same value while (start <end) & (num [start] = num [start-1]) + + start; while (start <end) & (num [end] = num [end + 1]) -- end ;}}}}}} return result ;}}
          
         
        
       
    class Solution {public:    vector
       
         > fourSum(vector
        
          &num, int target) {        // Note: The Solution object is instantiated only once.        vector
         
          > res;    int numlen = num.size();if(num.size()<4) return res;sort(num.begin(),num.end());set
          
           > tmpres;for(int i = 0; i < numlen; i++){for(int j = i+1; j < numlen; j++){int begin = j+1;int end = numlen-1;while(begin < end){int sum = num[i]+ num[j] + num[begin] + num[end];if(sum == target){vector
           
             tmp;tmp.push_back(num[i]);tmp.push_back(num[j]);tmp.push_back(num[begin]);tmp.push_back(num[end]);tmpres.insert(tmp);begin++;end--;}else if(sum
            
             >::iterator it = tmpres.begin();for(; it != tmpres.end(); it++)res.push_back(*it);return res; }};
            
           
          
         
        
       


     

     

     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.