-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlce_p0001_two_sum.java
More file actions
74 lines (54 loc) · 1.65 KB
/
lce_p0001_two_sum.java
File metadata and controls
74 lines (54 loc) · 1.65 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
LCE 1. Two Sum
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Constraints:
- 2 <= nums.length <= 10^4
- -10^9 <= nums[i] <= 10^9
- -10^9 <= target <= 10^9
- Only one valid answer exists.
Topics:
- Array
- Hash Table
*/
import java.util.HashMap;
import java.util.Map;
class Solution {
// Time Complexity: O(n) - 2 ms -> 98.88%
// Space Complexity: O(n) - 44.8 MB -> 48.36%
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> indexMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
int diff = target - num;
if (indexMap.containsKey(diff)) {
return new int[] { indexMap.get(diff), i };
}
indexMap.put(num, i);
}
return new int[] { -1, -1 };
}
// Time Complexity: O(n) - 4 ms -> 60.41%
// Space Complexity: O(n) - 44.3 MB -> 97.08%
public int[] twoSumAlt(int[] nums, int target) {
int n = nums.length;
Map<Integer, Integer> indexMap = new HashMap<>();
for (int i = 0; i < n; i++) {
indexMap.put(nums[i], i);
}
for (int i = 0; i < n; i++) {
int num = nums[i];
int diff = target - num;
if (indexMap.containsKey(diff) && i != indexMap.get(diff)) {
return new int[] { i, indexMap.get(diff) };
}
}
return new int[] { -1, -1 };
}
}
/*
methods:
1. hash map - single pass (i - larger index)
2. hash map - double pass (i - smaller index)
*/