Skip to content

Commit 7bd6931

Browse files
author
harshbhardwaj
committed
Add Letcode amazon questions
1 parent f674d35 commit 7bd6931

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.practise;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class TwoSum {
7+
public static Integer[] twoSum(int[] nums, Integer target) {
8+
9+
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
10+
for(int i =0; i< nums.length ;i++){
11+
if(!map.containsKey(nums[i])){
12+
map.put(nums[i], i);
13+
}
14+
}
15+
16+
Integer array[] = new Integer[2];
17+
18+
for (Map.Entry<Integer,Integer> entry : map.entrySet()) {
19+
20+
int key = entry.getKey();
21+
if(map.containsKey(target - key)){
22+
23+
array[0] = map.get(key);
24+
array[1] = map.get(target-key);
25+
}
26+
}
27+
return array;
28+
}
29+
30+
public static void main(String[] args) {
31+
TwoSum.twoSum(new int[]{2, 7, 11, 15},9);
32+
}
33+
}

0 commit comments

Comments
 (0)