-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2Sum.java
30 lines (24 loc) · 968 Bytes
/
2Sum.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
// https://leetcode.com/problems/two-sum/
// https://youtu.be/KLlXCFG5TnA?si=oLf1UDpoxwg9AA9w
/*
Time Complexity: O(n) where n = length of nums
Space Complexity: O(m) where m = size of hash map
*/
class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> complements = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int curElem = nums[i];
if (complements.containsKey(curElem)) {
int first_idx = complements.get(curElem).intValue();
return new int[]{first_idx, i};
}
Integer remainder = Integer.valueOf(target - curElem);
complements.put(remainder, Integer.valueOf(i));
}
// IMPORTANT: Problem said that there's a guaranteed answer per input
// and I needed to return something because of function's declaration,
// so this was added to silent error.
return null;
}
}