题解 | #两数之和#
两数之和
http://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f
leetcode 第一题 差点没通过
import java.util.*;
public class Solution {
/**
*
* @param numbers int整型一维数组
* @param target int整型
* @return int整型一维数组
*/
public int[] twoSum (int[] numbers, int target) {
// write code here
Map<Integer, Integer> tmp = new HashMap<>();
for (int i = 0; i < numbers.length; i++) {
if (tmp.containsKey(target - numbers[i])) {
return new int[]{tmp.get(target - numbers[i]), i + 1};
} else {
tmp.put(numbers[i], i + 1);
}
}
return new int[2];
}
}