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
Given a set of integers, find two numbers A and B, making the a+b= target number. Output the index of these two numbers, small to large, and be careful not to count starting from 0, as shown below. You can assume there is only one solution.
1 Public Static int[] Twosum (int[] numbers,inttarget) {2 int[] res =New int[2];3Hashmap<integer,integer> nums =NewHashmap<integer,integer>();4 for(inti=0;i<numbers.length;i++){5Integer A =Nums.get (Numbers[i]);6 if(A = =NULL){7 Nums.put (Numbers[i], i);8 }9A = Nums.get (Target-numbers[i]);Ten if(a!=NULL&&a<i) { One //The instructions found the solution. ARes[0] = a+1; -RES[1] = i+1; - Break; the}Else{ -Res[0] = 1; -RES[1] = 1; - } + } - returnRes; + A}View Code
Leetcode001:twosum (5.5)