Symbols count in article: 385Reading time ≈1 mins.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> map = new HashMap<>(); for(int i = 0; i < nums.length; i++) { int n = target - nums[i]; if(map.containsKey(n)) { return new int[] {map.get(n), i}; } map.put(nums[i], i); } throw new IllegalArgumentException("No two sum solution"); } }