Starting today, every day together leetcode!
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, target=9
Output: index1=1, index2=2
classSolution { Public: Vector<int> Twosum (vector<int> &numbers,inttarget) { inti, sum; Vector<int>results; Map<int,int>Hmap; for(i=0; I<numbers.size (); i++){ if(!Hmap.count (Numbers[i])) {Hmap.insert (pair<int,int>(Numbers[i], i)); } if(Hmap.count (target-Numbers[i])) { intn=hmap[target-Numbers[i]]; if(n<i) {Results.push_back (n+1); Results.push_back (i+1); returnresults; } } } returnresults; }};
The Sum of