[Link] https://leetcode.com/problems/two-sum/
1
2
3
4
5
6
7
8
9
10
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> hm = new HashMap<>();
for(int i = 0; i < nums.length; i++) hm.put(nums[i], i);
for(int i = 0; i < nums.length; i++)
if(hm.containsKey(target - nums[i]) && (i != hm.get(target-nums[i])))
return new int[] { i, hm.get(target - nums[i]) };
return null;
}
}