-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
56 lines (44 loc) · 1.58 KB
/
TwoSum.java
File metadata and controls
56 lines (44 loc) · 1.58 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
/*
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.
*/
import java.util.*;
import java.util.List;
class TwoSum {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> nums = new ArrayList<>();
System.out.println("Provide an array, a target, and a non-integer char to run program.");
while (scanner.hasNextInt()) {
nums.add(scanner.nextInt());
}
int target = nums.get(nums.size() - 1);
nums.remove(nums.size() - 1);
scanner.close();
System.out.println(
checkSums(nums, target));
}
private static List<Integer> checkSums(List<Integer> nums, int target) {
Map<Integer, Integer> remainderMap = new HashMap<>();
for (int i = 0, limit = nums.size(); i < limit; i++) {
Integer remainder = target - nums.get(i);
if (remainderMap.containsKey(nums.get(i))) {
return List.of(remainderMap.get(nums.get(i)), i);
}
remainderMap.put(remainder, i);
}
System.out.print("Cannot found complementing pairs on this array: ");
return nums;
}
}
/*
input: 2 7 11 15 9 .
output: [0, 1]
input: 3 2 4 6 .
output: [1, 2]
input: 2 3 4 5 6 14 .
output: Cannot found complementing pairs on this array: [2, 3, 4, 5, 6]
input: 3 3 6 .
output: [0, 1]
*/