1.两数之和
利用HashMap,Key存数字,Value存下表。
class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer,Integer> Hashtable = new HashMap<Integer,Integer>(); for(int i = 0; i < nums.length ; i++){ int y = target - nums[i]; if(Hashtable.containsKey(y)){ return new int[]{i+1,Hashtable.get(y)+1}; } Hashtable.put(nums[i],i); } return new int[0]; } }
浙公网安备 33010602011771号