Skip to content

Commit 5a1ee89

Browse files
authored
solution two sum
1 parent 4c8b68f commit 5a1ee89

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

two-sum/river20s.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* T.C: O(n) -> 배열 nums를 한 번 순회
3+
* S.C: O(n) → 최대 n개의 요소가 저장됨
4+
*/
5+
import java.util.HashMap;
6+
7+
class Solution {
8+
public int[] twoSum(int[] nums, int target) {
9+
10+
HashMap<Integer, Integer> map = new HashMap<>();
11+
for (int i = 0; i < nums.length; i++) {
12+
int k = target - nums[i];
13+
14+
if (map.containsKey(k)) {
15+
return new int[] { map.get(k), i };
16+
}
17+
18+
map.put(nums[i], i);
19+
}
20+
throw new IllegalArgumentException("exception handling for java compilation");
21+
22+
}
23+
}
24+

0 commit comments

Comments
 (0)