Given an array of integers, find the numbers such that they add up to a specific target number.
The function twosum should return indices of the numbers such that they add up to the target, where index1 must is Les S than Index2. Please note that your returned answers (both Index1 and INDEX2) is not zero-based.
You may assume this each input would has exactly one solution.
Input:numbers={2, 7, one, A, target=9
Output:index1=1, index2=2
IDEA: Data structure map, record the remaining value
classSolution { Public: Vector<int> Twosum (vector<int> &numbers,inttarget) {Vector<int>Res; //check validation; if(Numbers.empty ())returnRes; //Check special case or bound;size_t n=numbers.size (); if(n==1)returnRes; // General caseunordered_map<int,int>map; intrest=0; for(intI=0; i<n;i++){ //Check if find the number if(Map.count (Numbers[i])) {res.push_back (map[numbers[i]); Res.push_back (i+1); Break; } //compute the number[i] ' s restRest = target-Numbers[i]; Map[rest]=i+1; } returnRes; }};
The Sum of