DEV Community

realNameHidden
realNameHidden

Posted on

1. Two Sum leetcode Code in java

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 :

Top comments (0)