两数之和
两数之和
http://www.nowcoder.com/questionTerminal/20ef0972485e41019e39543e8e895b7f
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> mem = new HashMap<>(); for (int i = 0; i < numbers.length; i++) { int another = target - numbers[i]; if (mem.containsKey(another) && mem.get(another) != i) { return new int[]{mem.get(another) + 1, i + 1}; } else mem.put(numbers[i], i); } return new int[]{}; } }