-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReturnIndicesOfTwoSum.java
More file actions
62 lines (47 loc) · 1.7 KB
/
Copy pathReturnIndicesOfTwoSum.java
File metadata and controls
62 lines (47 loc) · 1.7 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
package array;
import util.Util;
import java.util.Arrays;
import java.util.HashMap;
/**
* Question:
* Return the indices of the two numbers so that they add up to target given an array of integers nums and an integer target.
* You can make an assumption that every input has exactly one solution, and you may avoid using the same element more than once.
* Note: Return the answer in sorted order.
*
* nums:[ 1, 2, 3, 4, 5]
* target: 8;
* output:[2, 4]
*
*/
public class ReturnIndicesOfTwoSum {
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 5};
int target = 8;
// int[] result = twoSumIndices(nums, target);
int[] result = twoSumIndicesUsingTwoPointer(nums, target);
Util.printArray(result);
}
static int[] twoSumIndices(int[] nums, int target) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int compliment = target - nums[i];
if (map.containsKey(compliment)) {
return new int[]{map.get(compliment), i};
}
map.put(nums[i], i);
}
return new int[0];
}
static int[] twoSumIndicesUsingTwoPointer(int[] nums, int target){
// create a new array of pairs (value, index)
int[][] numWithIndex = new int[nums.length][2];
// Fill the array with values and their corresponding indices
for (int i = 0; i < nums.length ; i++) {
numWithIndex[i][0] = nums[i];
numWithIndex[i][1] = i;
}
// Sort the array based on values (nums)
Arrays.sort(numWithIndex, (a, b) -> Integer.compare(a[0], b[0]));
return new int[0];
}
}