3Sum
Title Description: Given an array of integers, find out the sum of the three numbers in all combinations of 0 (excluding the same combination).
Analysis: Using the idea of two pointers in Twosum, we can sort the arrays first. To find 3 numbers of sum of 0, we can first fix a number num[i], the i+1 and len-1 respectively as the head pointer and the tail pointer, when Num[i], num[i+1] and num[len-1] and greater than 0 o'clock, moving the tail pointer; less than 0 o'clock, moving the head pointer; equals 0 finds a group of numbers. When you loop through the next set of solutions, a duplicate solution appears when the next value is equal to the current value, and you can skip the current loop to remove the repetition. The specific code is as follows:
1vector<vector<int>> Threesum (vector<int>&nums) {2 sort (Nums.begin (), Nums.end ());3 intlen=nums.size ();4vector<vector<int> >Res;5vector<int>mid;6 for(intI=0; i<len-2; i++)7 {8 if(nums[i]>0)returnRes;9 //prevent duplicates from appearingTen if(i>0&& Nums[i] = = Nums[i-1])Continue; One intStart= i+1, end = len-1; A while(start<end) - { - if((Nums[start] + nums[end] +nums[i]) <0) start++; the Else if((Nums[start] + nums[end] +nums[i]) >0) end--; - Else - { - Mid.push_back (Nums[i]); +Mid.push_back (nums[start++]); -Mid.push_back (nums[end--]); + Res.push_back (mid); A mid.clear (); at //prevent duplicates from appearing - while(Start<end&&nums[start] = = nums[start-1]) start++; - while(Start<end&&nums[end] = = nums[end+1]) end--; - } - } - } in returnRes; -}
3Sum Closest
Given an array, find the sum of three numbers closest to the target value, and return this sum. For example, given array s = {-1 2 1-4}, and target = 1. The most recent target-like value is 2 (-1 + 2 + 1 = 2).
This method is similar to the previous one, each time the loop is to record the difference between the sum value and the target value, and save the sum value, if the sum value is equal to the target value, it is returned directly. The code is as follows:
1 intThreesumclosest (vector<int>& Nums,inttarget) {2 sort (Nums.begin (), Nums.end ());3 intlen=nums.size ();4 intres=0, result=0;5 intdist=Int_max;6 for(intI=0; i<len-2; i++)7 {8 if(i>0&& Nums[i] = = Nums[i-1])Continue;9 intStart= i+1, end = len-1;Ten while(start<end) One { ARes=nums[start] + nums[end] +Nums[i]; - if(res<target) - { the if(target-res<Dist) - { -result=Res; -dist=target-Res; + } -start++; + while(Start<end&&nums[start] = = nums[start-1]) start++; A } at Else if(res>target) - { - if(res-target<Dist) - { -result=Res; -dist=res-Target; in } -end--; to while(Start<end&&nums[end] = = nums[end+1]) end--; + } - Else the returnRes; * } $ }Panax Notoginseng returnresult; -}
4Sum
Find the four-digit a,b,c,d in the array, making the a+b+c+d=target. This is the same as 3Sum, but we have to fix two numbers first, so we have a loop, and for the sake of convenience, we remember sum=target-a-b, comparing sum with A+B, the code is as follows:
1vector<vector<int>> Foursum (vector<int>& Nums,inttarget) {2 sort (Nums.begin (), Nums.end ());3 intlen=nums.size ();4vector<vector<int> >Res;5vector<int>mid;6 for(intI=0; i<len-3; i++)7 {8 if(i>0&& Nums[i] = = Nums[i-1])Continue;9 for(intj=i+1; j<len-2; j + +)Ten { One if(j>i+1&& Nums[j] = = Nums[j-1])Continue; A intStart= j+1, end = len-1, sum=target-nums[i]-Nums[j]; - while(start<end) - { the if((Nums[start] + nums[end]) <sum) start++; - Else if((Nums[start] + nums[end]) >sum) end--; - Else - { + Mid.push_back (Nums[i]); - Mid.push_back (Nums[j]); +Mid.push_back (nums[start++]); AMid.push_back (nums[end--]); at Res.push_back (mid); - mid.clear (); - while(Start<end&&nums[start] = = nums[start-1]) start++; - while(Start<end&&nums[end] = = nums[end+1]) end--; - } - } in } - } to returnRes; +}
3Sum and other similar problems analysis