利用map的key,value一一对应的特点来实现
时间O(n),空间O(n)
public int[] twoSum(int[] nums, int target) { HashMap<Integer,Integer> map = new HashMap<Integer,Integer>(); for (int i=0;i<nums.length;i++){ if(map.containsKey(target-nums[i])){ return new int[]{map.get(target-nums[i]),i}; }else { map.put(nums[i],i); } } return new int[]{0,0}; }
原文:https://www.cnblogs.com/jchen104/p/14591342.html