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
Direct Violence Law O (n*n) timeout, so the use of HashMap, where map to store data and search, in fact, is now
1 classSolution {2 Public:3vector<int> Twosum (vector<int>& Nums,inttarget) {4vector<int>Res;5map<int,int>tmp;6 for(intI=0; I<nums.size (); i++)7 {8 if(!Tmp.count (Nums[i]))9Tmp.insert (pair<int,int> (nums[i],i+1));Ten if(Tmp.count (target-Nums[i])) One { A intn=tmp[target-Nums[i]]; - if(n<i+1) - { the res.push_back (n); -Res.push_back (i+1); - returnRes; - } + } - } + returnRes; A } at};
Red and black trees.
The Sum of