From c564f14dd149ad2de45cf157b87b5b3cb3364b95 Mon Sep 17 00:00:00 2001 From: Xevion Date: Tue, 12 Jan 2021 19:26:40 -0600 Subject: [PATCH] two-sum solution java --- two-sum/Solution.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 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