1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| class Solution { public int[] twoSum(int[] nums, int target) { HashMap<Integer, Integer> tracker = new HashMap<Integer, Integer>(); int len = nums.length; for(int i = 0; i < len; i++){ if(tracker.containsKey(nums[i])){ int left = tracker.get(nums[i]); return new int[]{left, i}; }else{ tracker.put(target - nums[i], i); } } return new int[2]; } }
|