File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .HashMap ;
2
+ import java .util .Map ;
3
+
4
+ public class Geegong {
5
+
6
+ /**
7
+ * time complexity : O(n)
8
+ * space complexity : O(n)
9
+ * @param nums
10
+ * @param target
11
+ * @return
12
+ */
13
+ public int [] twoSum (int [] nums , int target ) {
14
+ Map <Integer , Integer > map = new HashMap <>();
15
+ int [] result = new int [2 ];
16
+
17
+ // if target = -9 / num = 1 , num = -10
18
+ for (int index =0 ; index <nums .length ; index ++) {
19
+ map .put (nums [index ], index );
20
+ }
21
+
22
+ for (int index =0 ; index <nums .length ; index ++) {
23
+ int difference = target - nums [index ];
24
+
25
+ if (map .containsKey (difference )
26
+ && map .get (difference ) != index ) {
27
+ result [0 ] = index ;
28
+ result [1 ] = map .get (difference );
29
+ return result ;
30
+ }
31
+ }
32
+
33
+ return result ;
34
+ }
35
+ }
You can’t perform that action at this time.
0 commit comments