-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwoSum2.java
39 lines (33 loc) · 865 Bytes
/
TwoSum2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package array;
import java.util.*;
/**
* @author Shogo Akiyama
* Solved on 12/31/2019 and 05/21/2020
* Time: 5m
*
* 1. Two Sum
* https://leetcode.com/problems/two-sum/
* Difficulty: Easy
*
* Approach: Hash Map
* Runtime: 2 ms, faster than 98.86% of Java online submissions for Two Sum.
* Memory Usage: 38.3 MB, less than 77.56% of Java online submissions for Two Sum.
*
* Time Complexity: O(n)
* Space Complexity: O(n)
* Where n is the number of elements in the array
*
* @see ArrayTest#testTwoSum()
*/
public class TwoSum2 {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) {
return new int[] { map.get(target - nums[i]), i };
}
map.put(nums[i], i);
}
return new int[2];
}
}