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 1111// 3. 똑같은 원소를 두번 사용하지 못하고, 정확히 하나의 정답만 있다.
1212
1313class Solution {
14- // Solv2 : map
14+ // Solv3 : map 최적화
1515 // 시간복잡도 : O(n)
1616 // 공간복잡도 : O(n)
1717 public int [] twoSum (int [] nums , int target ) {
1818 Map <Integer , Integer > map = new HashMap <>();
1919 int [] result = new int [2 ];
20- for (int i = 0 ; i < nums .length ; i ++) {
21- map .put (nums [i ], i );
22- }
23-
2420 for (int i = 0 ; i < nums .length ; i ++) {
2521 int key = target - nums [i ];
2622 if (map .containsKey (key ) && map .get (key ) != i ) {
2723 result [0 ] = i ;
2824 result [1 ] = map .get (key );
2925 }
26+ map .put (nums [i ], i );
3027 }
3128 return result ;
32-
3329 }
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+ // }
3451//-------------------------------------------------------------------------------------------------------------
3552// Solv1: Brute Force
3653// 시간복잡도 : O(n^2)
You can’t perform that action at this time.
0 commit comments