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
Train of thought: This question has no idea ~ Read the question carefully! No binary search
1 Public int[] Twosum (int[] numbers,inttarget) {2 int[] res = {-1,-1};3 if(Numbers = =NULL|| Numbers.length < 2) {4 returnRes;5 }6Hashmap<integer, integer> map =NewHashmap<integer, integer>();7 for(inti = 0; i < numbers.length; i++) {8 intval = target-Numbers[i];9 if(Map.containskey (val)) {Ten return New int[] {Map.get (val) + 1, i + 1}; One}Else{ A Map.put (Numbers[i], i); - } - } the returnRes; -}
The Sum of