/* * 1.Two Sum * 2016-4-8 by Mingyang * 注意HashMap的containsKey,put的用法 */ public int[] twoSum(int[] nums, int target) { int[] res=new int[2]; if(nums.length==0||nums==null) return res; HashMap<Integer,Integer> map=new HashMap<Integer,Integer>(); for(int i=0;i<nums.length;i++){ if(map.containsKey(nums[i])){ res[0]=map.get(nums[i]); res[1]=i; }else{ int temp=target-nums[i]; map.put(temp,i); } } return res; }