Skip to content

Commit ed585d2

Browse files
authored
Create Two Sum.java
1 parent 91d97be commit ed585d2

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Array/Hash Table/Two Sum.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int[] twoSum(int[] nums, int target) {
3+
4+
Map<Integer, Integer> mp = new HashMap<>();
5+
int[] res = new int[2];
6+
for (int i = 0; i < nums.length; i++) {
7+
if (mp.containsKey(nums[i])) {
8+
int index = mp.get(nums[i]);
9+
res[0] = index;
10+
res[1] = i;
11+
} else {
12+
mp.put(target-nums[i], i);
13+
}
14+
}
15+
return res;
16+
}
17+
}
18+

0 commit comments

Comments
 (0)