-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoSumArray.java
47 lines (36 loc) · 1.08 KB
/
TwoSumArray.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
40
41
42
43
44
45
46
47
public class TwoSumArray {
// Time complexity: O(n2)
// Space Complexity: O(1)
public static int[] twoSum(int[] nums, int target) {
for(int i = 0; i < nums.length - 1; i++){
for(int j = i + 1; j < nums.length; j++){
if(nums[i] + nums[j] == target){
return new int[] {i, j};
}
}
}
return null;
}
/*
* Better alternative with
*
* Time complexity: O(n)
* Space Complexity: O(n)
*
* public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> prevMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int num = nums[i];
int diff = target - num;
if (prevMap.containsKey(diff)) {
return new int[] { prevMap.get(diff), i };
}
prevMap.put(num, i);
}
return new int[] {};
}
*/
public static void main(String[] args) {
System.out.println(twoSum(new int[] {3, 4, 5, 6}, 7).toString());
}
}