Last active
June 28, 2019 11:43
-
-
Save Desolve/1b5deb87574dc16aa0a51d9a07a9fb2c to your computer and use it in GitHub Desktop.
0001 Two Sum
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) { | |
Map<Integer, Integer> map = new HashMap<>(); | |
for (int i = 0; i < nums.length; i++) { | |
int complement = target - nums[i]; | |
if(map.containsKey(complement)) { | |
return new int[] { map.get(complement), i}; | |
} | |
map.put(nums[i], i); | |
} | |
throw new IllegalArgumentException("No Answer"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment