Skip to content

Instantly share code, notes, and snippets.

@aonurdemir
Created November 19, 2019 21:49
Show Gist options
  • Save aonurdemir/3617ca06547796a13f7b1704aa668a7d to your computer and use it in GitHub Desktop.
Save aonurdemir/3617ca06547796a13f7b1704aa668a7d to your computer and use it in GitHub Desktop.
2sum
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> hash = new HashMap<Integer,Integer>();
for(int i=0; i < nums.length; i++){
int searched = target - nums[i];
if(hash.containsKey(searched)){
return new int[] {hash.get(searched),i};
}
else{
hash.put(nums[i],i);
}
}
throw new IllegalArgumentException("No two sum solution");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment