-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathtwosum.java
48 lines (39 loc) · 1.77 KB
/
twosum.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
48
import java.util.HashMap;
import java.util.Map;
class Solution {
public int[] twoSum(int[] nums, int target) {
// Create a HashMap to store the number and its index
Map<Integer, Integer> map = new HashMap<>();
// Iterate through the array
for (int i = 0; i < nums.length; i++) {
// Calculate the complement of the current element
int complement = target - nums[i];
// Check if the complement exists in the map
if (map.containsKey(complement)) {
// If it exists, return the indices of the complement and the current element
return new int[] { map.get(complement), i };
}
// Otherwise, add the current element and its index to the map
map.put(nums[i], i);
}
// If no solution is found, throw an exception (should not happen as per problem constraints)
throw new IllegalArgumentException("No two sum solution exists");
}
public static void main(String[] args) {
// Create an instance of the Solution class
Solution solution = new Solution();
// Test cases
int[] nums1 = {2, 7, 11, 15};
int target1 = 9;
int[] result1 = solution.twoSum(nums1, target1);
System.out.println("Output: [" + result1[0] + "," + result1[1] + "]");
int[] nums2 = {3, 2, 4};
int target2 = 6;
int[] result2 = solution.twoSum(nums2, target2);
System.out.println("Output: [" + result2[0] + "," + result2[1] + "]");
int[] nums3 = {3, 3};
int target3 = 6;
int[] result3 = solution.twoSum(nums3, target3);
System.out.println("Output: [" + result3[0] + "," + result3[1] + "]");
}
}