Skip to content

Commit e2a17ce

Browse files
dohee789dohee789
authored andcommitted
⚡ improve two-sum solution
1 parent b406f6f commit e2a17ce

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

two-sum/dohee789.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ public int[] twoSum(int[] nums, int target) {
66
Map<Integer, Integer> map = new HashMap<>();
77

88
for(int i = 0; i < nums.length; i++) {
9-
map.put(nums[i], i);
10-
}
11-
12-
for(int i = 0; i < nums.length; i++) {
13-
if(map.containsKey(target - nums[i]) && map.get(target - nums[i]) != i) {
9+
if(map.containsKey(target - nums[i])) {
1410
int j = map.get(target - nums[i]);
1511
return new int[]{i, j};
1612
}
13+
map.put(nums[i], i);
1714
}
1815

1916
return null;
2017
}
2118
}
19+

0 commit comments

Comments
 (0)