File tree Expand file tree Collapse file tree 1 file changed +23
-6
lines changed Expand file tree Collapse file tree 1 file changed +23
-6
lines changed Original file line number Diff line number Diff line change 11
11
// 3. 똑같은 원소를 두번 사용하지 못하고, 정확히 하나의 정답만 있다.
12
12
13
13
class Solution {
14
- // Solv2 : map
14
+ // Solv3 : map 최적화
15
15
// 시간복잡도 : O(n)
16
16
// 공간복잡도 : O(n)
17
17
public int [] twoSum (int [] nums , int target ) {
18
18
Map <Integer , Integer > map = new HashMap <>();
19
19
int [] result = new int [2 ];
20
- for (int i = 0 ; i < nums .length ; i ++) {
21
- map .put (nums [i ], i );
22
- }
23
-
24
20
for (int i = 0 ; i < nums .length ; i ++) {
25
21
int key = target - nums [i ];
26
22
if (map .containsKey (key ) && map .get (key ) != i ) {
27
23
result [0 ] = i ;
28
24
result [1 ] = map .get (key );
29
25
}
26
+ map .put (nums [i ], i );
30
27
}
31
28
return result ;
32
-
33
29
}
30
+ //-------------------------------------------------------------------------------------------------------------
31
+ // Solv2: map
32
+ // 시간복잡도 : O(n)
33
+ // 공간복잡도 : O(n)
34
+ // public int[] twoSum(int[] nums, int target) {
35
+ // Map<Integer, Integer> map = new HashMap<>();
36
+ // int[] result = new int[2];
37
+ // for (int i = 0; i < nums.length; i++) {
38
+ // map.put(nums[i], i);
39
+ // }
40
+ //
41
+ // for (int i = 0; i < nums.length; i++) {
42
+ // int key = target - nums[i];
43
+ // if (map.containsKey(key) && map.get(key) != i) {
44
+ // result[0] = i;
45
+ // result[1] = map.get(key);
46
+ // }
47
+ // }
48
+ // return result;
49
+ //
50
+ // }
34
51
//-------------------------------------------------------------------------------------------------------------
35
52
// Solv1: Brute Force
36
53
// 시간복잡도 : O(n^2)
You can’t perform that action at this time.
0 commit comments