Skip to content

Commit 8141c2d

Browse files
committed
1. Two Sum
1 parent 2712a2a commit 8141c2d

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

two-sum/TonyKim9401.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public int[] twoSum(int[] nums, int target) {
3+
// time complexity: O(n);
4+
// space complexity: O(n);
5+
Map<Integer, Integer> map = new HashMap<>();
6+
for (int i = 0; i < nums.length; i++) map.put(nums[i], i);
7+
8+
for (int i = 0; i < nums.length; i++) {
9+
int candidateKey = target - nums[i];
10+
if (map.containsKey(candidateKey) && i != map.get(candidateKey))
11+
return new int[]{i, map.get(candidateKey)};
12+
}
13+
return new int[]{-1, -1};
14+
}
15+
}

0 commit comments

Comments
 (0)