給定一個數組,而後找出數組中和爲target的兩個數數組
解題思想:主要用哈希表spa
class Solution { public int[] twoSum(int[] nums, int target) { int len = nums.length; Map<Integer,Integer> map = new HashMap<>(); for(int i=0;i<len;i++){ if(map.containsKey(target-nums[i])){ return new int[]{map.get(target-nums[i]),i}; }else{ map.put(nums[i],i); } } return null; } }