-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoSum1.java
More file actions
30 lines (27 loc) · 819 Bytes
/
twoSum1.java
File metadata and controls
30 lines (27 loc) · 819 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
30
// You get passed an array of numbers. Find the indices of two numbers that add upto a particlar target twoSum
// assume that there is only one solution
// input=[1,4,5,2],target=9
// output=[1,2] since 4+5=9
import java.util.HashMap;
class twoSum1{
public static int[] twoSum(int[] nums,int target){
HashMap<Integer,Integer> map= new HashMap<Integer,Integer>();
for(int i=0;i<nums.length;i++){
map.put(nums[i],i);
}
for(int i=0;i<nums.length;i++)
{
int complement=target-nums[i];
if(map.containsKey(complement) && map.get(complement)!=i)
return new int[] {i,map.get(complement)};
}
return new int[]{};
}
public static void main(String[] args){
int nums[]={2,4,3,5,7};
int target=8;
int ans[]=twoSum(nums,target);
for(int i:ans)
System.out.print(i+" "); //expected [2,3]
}
}