File tree Expand file tree Collapse file tree 1 file changed +8
-14
lines changed Expand file tree Collapse file tree 1 file changed +8
-14
lines changed Original file line number Diff line number Diff line change 2
2
import java .util .Map ;
3
3
4
4
class Solution {
5
- /**
6
- 공간복잡도: O(n)
7
- 시간복잡도: O(n)
8
- */
9
-
10
5
public int [] twoSum (int [] nums , int target ) {
11
- Map <Integer , Integer > numMap = new HashMap <>();
6
+ Map <Integer , Integer > indexMap = new HashMap <>();
12
7
13
8
for (int i = 0 ; i < nums .length ; i ++) {
14
- numMap .put (nums [i ], i );
15
- }
9
+ int remain = target - nums [i ];
16
10
17
- for (int i = 0 ; i < nums .length ; i ++) {
18
- int pairNum = target - nums [i ];
19
- if (numMap .containsKey (pairNum ) && numMap .get (pairNum ) != i ) {
20
- return new int []{i , numMap .get (target - nums [i ])};
11
+ if (indexMap .containsKey (remain )) {
12
+ return new int [] {i , indexMap .get (remain )};
21
13
}
14
+
15
+ indexMap .put (nums [i ], i );
22
16
}
23
17
24
- return new int [2 ] ;
18
+ return new int []{} ;
25
19
}
26
- }
20
+ }
You can’t perform that action at this time.
0 commit comments