diff --git a/0001_Two_Sum.java b/0001_Two_Sum.java new file mode 100644 index 0000000..01e47db --- /dev/null +++ b/0001_Two_Sum.java @@ -0,0 +1,20 @@ +// id: 1 +// Name: Two Sum +// link: https://leetcode.com/problems/two-sum/ +// Difficulty: Easy + +class Solution { + public int[] twoSum(int[] nums, int target) { + HashMap map = new HashMap<>(); + + for (int i = 0; i < nums.length; i++) { + if (map.containsKey(target-nums[i])) { + return new int[] {map.get(target-nums[i]), i}; + } + else { + map.put(nums[i], i); + } + } + return null; + } +}