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 f6fafb7 commit 6c9ac28Copy full SHA for 6c9ac28
solution/0000-0099/0001.Two Sum/java01.java
@@ -0,0 +1,29 @@
1
+import java.util.Arrays;
2
+import java.util.HashMap;
3
+import java.util.Map;
4
+
5
6
7
+public class java01 {
8
+ public int[] Sum(int[] nums, int target){
9
+ Map<Integer, Integer> d = new HashMap<>();
10
+ for (int i = 0;; ++i) {
11
+ int x = nums[i];
12
+ int y = target - x;
13
+ if (d.containsKey(y)) {
14
+ return new int[] {d.get(y), i};
15
+ }
16
+ d.put(x, i);
17
18
19
20
21
+ public static void main(String[] args){
22
+ java01 s = new java01();
23
+ int[] nums = {2, 7, 11, 15};
24
+ int target = 9;
25
+ int[] result = s.Sum(nums, target);
26
+ System.out.println("Result: "+ Arrays.toString(result));
27
28
29
+}
0 commit comments