We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 2712a2a commit 8141c2dCopy full SHA for 8141c2d
two-sum/TonyKim9401.java
@@ -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