1. Two Sum leetcode Code in java

realNameHidden - Oct 25 '22 - - Dev Community

class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> hm = new HashMap<>();
int n = nums.length;
for(int i=0;i<n;i++){
int diff = target-nums[i];
if(hm.containsKey(diff)){
return new int[] {hm.get(diff),i};
}
hm.put(nums[i],i);
}
return new int[] {-1,-1};
}
}

For Explanation Watch video :

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .