-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1-Two Sum.java
More file actions
47 lines (39 loc) · 1.35 KB
/
1-Two Sum.java
File metadata and controls
47 lines (39 loc) · 1.35 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
// Time complexity: O(n)
// Space complexity: O(n)
// hash table
import java.util.Map;
import java.util.HashMap;
class Solution {
public int[] twoSum(int[] nums, int target) {
// prepare a hash table to store the value
Map<Integer, Integer> complementMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
// find complement
int complement = target - nums[i];
if (complementMap.containsKey(complement)) {
// if complement exists in the map, return the indices of the two numbers
return new int[]{complementMap.get(complement), i};
}
// append the value and index to hash table
complementMap.put(nums[i], i);
}
// If no solution is found, return an empty array
return new int[0];
}
}
// Time complexity: O(n^2)
// Space complexity: O(1)
// brute force
class Solution2 {
public int[] twoSum(int[] nums, int target) {
for (int i =0; i < nums.length;i ++ ) {
for (int j = 0; j < nums.length;j++) {
// find the two number that fulfil the requirement
if (i != j && (nums[i] + nums[j] == target)) {
return new int[]{i,j};
}
}
}
return new int[0];
}
}