commit c564f14dd149ad2de45cf157b87b5b3cb3364b95 Author: Xevion Date: Tue Jan 12 19:26:40 2021 -0600 two-sum solution java diff --git a/two-sum/Solution.java b/two-sum/Solution.java new file mode 100644 index 0000000..a4705e1 --- /dev/null +++ b/two-sum/Solution.java @@ -0,0 +1,18 @@ +// Accepted +// Runtime: 0 ms +// Memory Usage: 39.7 MB +// Submitted: January 12th, 2021 + +class Solution { + public int[] twoSum(int[] nums, int target) { + Map 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 two sum solution"); + } +} \ No newline at end of file