Skip to content

Commit 9508bce

Browse files
committed
two sum solution
1 parent 214cb31 commit 9508bce

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

two-sum/ohgyulim.java

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

0 commit comments

Comments
 (0)