Skip to content

Commit f6ef448

Browse files
dohee789dohee789
authored andcommitted
👔 two-sum solution
1 parent 101b6e1 commit f6ef448

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

two-sum/dohee789.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
https://leetcode.com/problems/two-sum/
3+
*/
4+
class Solution {
5+
public int[] twoSum(int[] nums, int target) {
6+
Map<Integer, Integer> map = new HashMap<>();
7+
for(int i = 0; i < nums.length; i++){
8+
map.put(nums[i], i);
9+
}
10+
11+
for(int i = 0; i < nums.length; i++){
12+
if(map.containsKey(target - nums[i])
13+
&& map.get(target - nums[i]) != i){
14+
int j = map.get(target - nums[i]);
15+
return new int[]{i, j};
16+
}
17+
}
18+
return null;
19+
}
20+
}

0 commit comments

Comments
 (0)