标签:
public class Solution { public int[] twoSum(int[] nums, int target) { int[] res = new int[2]; if(nums == null || nums.length < 2) { return null; } HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>(); for (int i = 0; i < nums.length; i++) { hm.put(nums[i], i+1); } for (int i = 0; i < nums.length; i++) { if (hm.containsKey(target - nums[i])) { int index1 = i + 1; int index2 = hm.get(target - nums[i]); if (index1 == index2) { continue; } res[0] = index1; res[1] = index2; return res; } } return res; } }
标签:
原文地址:http://www.cnblogs.com/77rousongpai/p/4480826.html