[Leetcode] 4Sum

Source: Internet
Author: User

 

Given an 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 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)

 

Two elements are enumerated, and the next two elements adopt the 2sum double-pointer approach.

 

1st (9 tries)

Class solution {public: vector <int> foursum (vector <int> & num, int target) {// cubic algorithm with a time complexity of N, first locate the first two, and then the last two ideas that use 2 sum // deduplication may not be very good. Vector <vector <int> re; vector <int> tmpnum; if (Num. size () <4) return re; sort (Num. begin (), num. end (); For (INT I = 0; I <num. size ()-3; I ++) {if (I> 0 & num [I] = num [I-1]) continue; for (Int J = I + 1; j <num. size ()-2; j ++) {If (j> I + 1 & num [J] = num [J-1]) continue; tmpnum. clear (); tmpnum. push_back (Num [I]); tmpnum. push_back (Num [J]); int tmptarget = target-num [I]-num [J]; int start = J + 1; int end = num. size ()-1; while (start <End) {If (Num [start] + num [end] = tmptarget) {If (Start> J + 1 & End <num. size ()-1 & num [start] = num [start-1] & num [end] = num [end + 1]) {start ++; end --; continue;} tmpnum. push_back (Num [start]); tmpnum. push_back (Num [end]); Re. push_back (tmpnum); tmpnum. pop_back (); tmpnum. pop_back (); Start ++; end --;} else if (Num [start] + num [end]> tmptarget) {end --;} else {start ++ ;}}} return re ;}};

2nd (3 tries)

Class solution {public: vector <int> ans; vector <int> res; vector <int> foursum (vector <int> & num, int target) {int n = num. size (); sort (Num. begin (), num. end (); int _ 1, _ 2, _ 3, _ 4; for (_ 1 = 0; _ 1 <n-3; _ 1 + +) {If (_ 1> 0 & num [_ 1] = num [_ 1-1]) continue; For (_ 2 = _ 1 + 1; _ 2 <N-2; _ 2 + +) {If (_ 2> _ 1 + 1 & num [_ 2] = num [_ 2-1]) continue; _ 3 = _ 2 + 1; _ 4 = n-1; while (_ 3 <_ 4) {If (_ 3> _ 2 + 1 & _ 4 <n-1 & num [_ 3] = num [_ 3-1] & num [_ 4] = = num [_ 4 + 1]) {_ 3 ++; _ 4 --; continue ;} if (Num [_ 1] + num [_ 2] + num [_ 3] + num [_ 4] = target) {res. push_back (Num [_ 1]); Res. push_back (Num [_ 2]); Res. push_back (Num [_ 3]); Res. push_back (Num [_ 4]); ans. push_back (RES); Res. clear (); _ 3 ++; _ 4 --;} else if (Num [_ 1] + num [_ 2] + num [_ 3] + num [_ 4]> Target) {_ 4 --;} else {_ 3 ++ ;}}} return ans ;}};

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.