Skip to content

Commit e0a1922

Browse files
add: two-sum
1 parent f291ae8 commit e0a1922

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

two-sum/YoungSeok-Choi.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// time complexity: O(n);
2+
3+
// 특정 nums[i] 가 target이 되기위한 보수 (target - nums[i])를 candidate Map에서 찾으면 종료
4+
class Solution {
5+
public int[] twoSum(int[] nums, int target) {
6+
Map<Integer, Integer> candidate = new HashMap<>();
7+
8+
for(int i = 0; i < nums.length; i++) {
9+
10+
int diff = target - nums[i];
11+
12+
if(candidate.containsKey(diff)) {
13+
return new int[] { candidate.get(diff), i };
14+
}
15+
16+
candidate.put(nums[i], i);
17+
}
18+
return new int[0];
19+
}
20+
}

0 commit comments

Comments
 (0)