Skip to content

Commit

Permalink
solution two sum
Browse files Browse the repository at this point in the history
  • Loading branch information
river20s authored Dec 26, 2024
1 parent 4c8b68f commit 5a1ee89
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions two-sum/river20s.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* T.C: O(n) -> 배열 nums를 한 번 순회
* S.C: O(n) → 최대 n개의 요소가 저장됨
*/
import java.util.HashMap;

class Solution {
public int[] twoSum(int[] nums, int target) {

HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int k = target - nums[i];

if (map.containsKey(k)) {
return new int[] { map.get(k), i };
}

map.put(nums[i], i);
}
throw new IllegalArgumentException("exception handling for java compilation");

}
}

0 comments on commit 5a1ee89

Please sign in to comment.