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