Sum of two numbers
Given an integer array nums and a target value target, please find the two integers with and as the target value in the array and return their array subscripts. You can assume that each input corresponds to only one answer. However, you cannot reuse the same elements in this array.
Example: given nums = [2, 7, 11, 15], target = 9. Because nums[0] + nums[1] = 2 + 7 = 9
So [0, 1] is returned.
Analysis: Double finger needling.
class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector<int>dump (nums); vector<int> temp; if (nums.size() < 2) { return temp; } sort(nums.begin(), nums.end()); int i = 0; int j = nums.size() - 1; while(j>i) { if (nums[i] + nums[j] == target) { for (int index=0; index < dump.size(); index++) { if (dump[index] == nums[i]) { temp.push_back(index); break; } } for (int index = 0; index < dump.size(); index++) { if ((dump[index] == nums[j])&&(index!=temp[0])) { temp.push_back(index); break; } } break; } else if (nums[i] + nums[j] > target) { j--; } else { i++; } } return temp; } };
Sum of three numbers
Given a n array of N integers nums, judge whether there are three elements a, b, c in nums, so that a + b + c = 0? Find all triples that meet the conditions and are not repeated.
Note: the answer cannot contain duplicate triples.
Example: given array nums = [-1, 0, 1, 2, -1, -4], the required set of triples is:
[
[-1, 0, 1],
[-1, -1, 2]
]
Analysis: Double finger needling.
class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> res_final; sort(nums.begin(), nums.end()); if (nums.size() < 3 || nums[0] > 0 || nums[nums.size()-1] < 0) return res_final; for (size_t i = 0; i < nums.size(); ++i) { if (nums[i] > 0) break; if (i>0 && nums[i] == nums[i-1]) continue; int target = 0-nums[i]; int k = i+1; int m = nums.size()-1; while(k<m) { if (nums[k] > target) break; if ((k>i+1 && nums[k] == nums[k-1]) || nums[k]+nums[m] < target) { ++k; } else if ((m<nums.size()-1 && nums[m] == nums[m+1]) || nums[k]+nums[m] > target) { --m; } else { res_final.push_back(vector<int>{nums[i],nums[k],nums[m]}); ++k; } } } return res_final; } };
The closest sum of three
Given an array of n integers nums and a target value target. Find three integers in nums so that their sum is closest to target. Returns the sum of the three numbers. Assume that there is only one answer for each group of input.
For example, given the array nums = [-1, 2, 1, - 4], and target = 1. The sum of the three numbers closest to target is 2. (- 1 + 2 + 1 = 2)
Analysis: Double finger needling.
class Solution { public: int threeSumClosest(vector<int>& nums, int target) { if(nums.size()<3) return INT_MAX;//Input detection sort(nums.begin(),nums.end()); int res = nums[0]+nums[1]+nums[2];//Initialization for(int i =0;i<nums.size();i++){ int front = i+1,back = nums.size()-1; while(front < back){ if(abs(nums[i]+nums[front]+nums[back]-target)<abs(res-target)) res = nums[i]+nums[front]+nums[back]; if(nums[i]+nums[front]+nums[back]>target) back--; else if (nums[i]+nums[front]+nums[back]<target) front++; else if (nums[i]+nums[front]+nums[back]==target) return target; } } return res; } };
Sum of four numbers
Given a n array of N integers nums a n d a target value target, judge whether there are four elements a, b, c and D in nums, so that the value of a + b + c + d is equal to target? Find all quads that meet the conditions and are not repeated.
Note: the answer cannot contain duplicate quads.
Example: given array nums = [1, 0, -1, 0, -2, 2], and target = 0. The set of quads that meet the requirements is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
Analysis: the solution of the sum of three numbers plus a layer of circulation.
class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int target) { vector<vector<int>> res_final; int temptarget = 0; sort(nums.begin(), nums.end()); if (nums.size() < 4 ) return res_final; for (size_t i = 0; i < nums.size()-3; ++i) { if (i > 0 && nums[i] == nums[i - 1]) continue; for (size_t j = i + 1; j < nums.size() - 2; j++) { if (j-i > 1 && nums[j] == nums[j- 1]) continue; temptarget = target- nums[i]-nums[j]; int k = j + 1; int m = nums.size() - 1; while (k < m) { if ((k > j + 1 && nums[k] == nums[k - 1]) || nums[k] + nums[m] < temptarget) { ++k; } else if ((m < nums.size() - 1 && nums[m] == nums[m + 1]) || nums[k] + nums[m] > temptarget) { --m; } else { res_final.push_back(vector<int>{nums[i], nums[j], nums[k], nums[m]}); ++k; } } } } return res_final; } };