题解 | #两数之和#
两数之和
http://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f
import java.util.*;
public class Solution {
/**
*
* @param numbers int整型一维数组
* @param target int整型
* @return int整型一维数组
*/
public int[] twoSum(int[] numbers, int target) {
int[] res = new int[2];//对应数组下标
HashMap map = new HashMap();
for (int i = 0; i < numbers.length; i++) {
map.put(numbers[i], i);
}
for (int i = 0; i < numbers.length; i++) {
if (map.get(target - numbers[i]) != null
&& (int)map.get(target - numbers[i]) != i) {
res[0] = i + 1;
res[1] = ((int) map.get(target - numbers[i])) + 1;
break;
}
}
return res;
}
}