一遍Hash即可完成任务
两数之和
http://www.nowcoder.com/questionTerminal/20ef0972485e41019e39543e8e895b7f
import java.util.*; public class Solution { public int[] twoSum (int[] numbers, int target) { int[] result = new int[2]; Map<Integer, Integer> map = new HashMap(); for(int i = 0; i < numbers.length; i++) { if(map.get(target - numbers[i]) != null) { result[0] = map.get(target - numbers[i]) + 1; result[1] = i + 1; return result; } map.put(numbers[i], i); } return result; } }