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 cc29a91 commit 151f5c1Copy full SHA for 151f5c1
two-sum/ymir0916.java
@@ -0,0 +1,16 @@
1
+import java.util.HashMap;
2
+import java.util.Map;
3
+
4
+class Solution {
5
+ public int[] twoSum(int[] nums, int target) {
6
+ Map<Integer, Integer> idx = new HashMap<>();
7
+ for (int i = 0; i < nums.length; i++) {
8
+ int c = target - nums[i];
9
+ if (idx.containsKey(c)) {
10
+ return new int[]{idx.get(c), i};
11
+ }
12
+ idx.put(nums[i], i);
13
14
+ return new int[]{};
15
16
+}
0 commit comments