-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathSolution.java
More file actions
29 lines (24 loc) · 818 Bytes
/
Solution.java
File metadata and controls
29 lines (24 loc) · 818 Bytes
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
import java.util.*;
class Solution {
public static int[] twoSum(int[] nums, int target)
{
Map<Integer,Integer> m = new HashMap<>();
for(int i = 0; i < nums.length; i++){
m.put(nums[i],i);
}
System.out.println(m.size());
for(int i = 0; i < nums.length; i++)
{
if(m.containsValue(target - nums[i] )&& m.get(target - nums[i]) != i)
{
int n[] = new int[]{ m.get(target - nums[i]), i};
return n;
}
}
throw new IllegalArgumentException("No two sum solution");
}
public static void main(String[] args) {
int nums[] = new int[]{3,3};
System.out.println(twoSum(nums, 6));
}
}