Skip to content

Commit 39fc7a9

Browse files
committed
🎨 TwoSum Solution
1 parent 4c8b68f commit 39fc7a9

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

two-sum/dalpang81.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* 시간복잡도 : O(n)
3+
* 공간복잡도 : O(n)
4+
* */
5+
6+
import java.util.HashMap;
7+
import java.util.Map;
8+
9+
class Solution {
10+
public int[] twoSum(int[] nums, int target) {
11+
Map<Integer, Integer> map = new HashMap<>();
12+
13+
for (int i = 0; i < nums.length; i++) {
14+
int complement = target - nums[i];
15+
16+
if (map.containsKey(complement)) {
17+
return new int[] { map.get(complement), i };
18+
}
19+
map.put(nums[i], i);
20+
}
21+
return null;
22+
}
23+
}

0 commit comments

Comments
 (0)