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
Source: >
Solution1:
Hash Table
unordered_map<int, int> hash;
vector<int> result;
Span class= "KWD" >for ( int i = 0 I < Input_nums size (); I ++) {
int numberToFind = target - input_nums[i];
//if numberToFind is found in map, return them
if (hash.find(numberToFind) != hash.end()) {
//+1 because indices are NOT zero based
result.push_back(hash[numberToFind] + 1);
result.push_back(i + 1);
}
//number was not found. Put it in the map.
hash[input_nums[i]] = i;
}
From for notes (Wiz)
Both Sum