Created
November 19, 2019 21:49
-
-
Save aonurdemir/3617ca06547796a13f7b1704aa668a7d to your computer and use it in GitHub Desktop.
2sum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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