Skip to content

Commit fe794af

Browse files
committed
solve two sum problem
1 parent 3568b91 commit fe794af

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

two-sum/sora0319.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
11
import java.util.*;
2+
3+
// 개선안
4+
class Solution {
5+
public int[] twoSum(int[] nums, int target) {
6+
Map<Integer,Integer> checkNums = new HashMap<>();
7+
8+
for(int i = 0; i < nums.length; i++){
9+
if(checkNums.containsKey(target - nums[i])){
10+
int place = checkNums.get(target - nums[i]);
11+
return new int[]{place, i};
12+
}
13+
checkNums.put(nums[i], i);
14+
}
15+
return new int[]{};
16+
}
17+
}
18+
19+
20+
// 초기 구성안
221
class Solution {
322
public int[] twoSum(int[] nums, int target) {
423
Map<Integer,Integer> element = new HashMap<>();

0 commit comments

Comments
 (0)