Skip to content

Commit 95bdea2

Browse files
committed
two sum solved
1 parent 6c3f595 commit 95bdea2

File tree

1 file changed

+8
-14
lines changed

1 file changed

+8
-14
lines changed

two-sum/mintheon.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,19 @@
22
import java.util.Map;
33

44
class Solution {
5-
/**
6-
공간복잡도: O(n)
7-
시간복잡도: O(n)
8-
*/
9-
105
public int[] twoSum(int[] nums, int target) {
11-
Map<Integer, Integer> numMap = new HashMap<>();
6+
Map<Integer, Integer> indexMap = new HashMap<>();
127

138
for(int i = 0; i < nums.length; i++) {
14-
numMap.put(nums[i], i);
15-
}
9+
int remain = target - nums[i];
1610

17-
for(int i = 0; i < nums.length; i++) {
18-
int pairNum = target - nums[i];
19-
if(numMap.containsKey(pairNum) && numMap.get(pairNum) != i) {
20-
return new int[]{i, numMap.get(target - nums[i])};
11+
if(indexMap.containsKey(remain)) {
12+
return new int[] {i, indexMap.get(remain)};
2113
}
14+
15+
indexMap.put(nums[i], i);
2216
}
2317

24-
return new int[2];
18+
return new int[]{};
2519
}
26-
}
20+
}

0 commit comments

Comments
 (0)