Code:
1#include <iostream>2#include <algorithm>3#include <vector>4 5 using namespacestd;6 7 intThreesumclosest (vector<int> &num,inttarget) {8 sort (Num.begin (), Num.end ());9 intL =num.size ();Ten intMin =Int_max; One intBeauty; A for(inti =0; I < L2; i++) - { - intL = i +1; the intr = L-1; - intsum = num[l]+num[r]+Num[i]; - while(L <R) - { +sum = Num[l] + num[r] +Num[i]; - if(ABS (Target-sum) <min) + { Amin = ABS (Target-sum); atBeauty =sum; - } - if(Sum <target) - { -l++; - } in Else if(Sum >target) - { tor--; + } - Else the { * returnsum; $ }Panax Notoginseng } - } the returnBeauty; + } A the + - intMain () $ { $vector<int> S = { the,6, - -, - +,Ten, -8, - -, About, -, -1, - the, - $, - -,Ten, -, -4, -, - -,0, One, - -, $, About, - -, -, - the, -, - -, the, - the, - +, - the, -, +, - About, -, A, the, - -, -2, -, - -, - -, the, the, -, - A, $, - the, $, - -, the, the, -, - +, - -, - -, -, - A, -, -,1, - the, One, - *, -7, A, -, -,98, in, the, the, - the, - the, - at, - the, the,3, - $, the, -, the, About, -7, +, -, - the, -,94, - +, - $, the, +, - A, - the, the,1, -Ten, - +, One, Wu, -Bayi, -, +, - in, the, the, $,Ten, - -, - the, - About, the, the, the, the, the, the, -, - -, A, -, the, - the, - -, One, - $, -, $, -, - the, the, - +,4 }; -cout << Threesumclosest (s,-275) <<Endl; - return 0; the}
Leetcode 3Sum Closest