File tree Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Expand file tree Collapse file tree 1 file changed +48
-0
lines changed Original file line number Diff line number Diff line change 1+ import java .util .Map ;
2+
3+ class Solution {
4+
5+ /*
6+ * 시간복잡ㄷ도 개선
7+ *
8+ * 시간복잡도 O(n)
9+ * 공간복잡도 O(n)
10+ */
11+ public int [] twoSum (int [] nums , int target ) {
12+ Map <Integer , Integer > arr = new HashMap <>();
13+
14+ for (int i =0 ; i <nums .length ; i ++) {
15+ if (arr .containsKey (target -nums [i ])) {
16+ return new int []{arr .get (target -nums [i ]), i };
17+ }
18+ arr .put (nums [i ], i );
19+ }
20+ return new int []{0 , 0 };
21+ }
22+
23+
24+
25+ /*
26+ * 기존 풀이
27+ *
28+ * 시간복잡도 O(n^2)
29+ * 공간복잡도 O(1)
30+ */
31+
32+ /*
33+ public int[] twoSum(int[] nums, int target) {
34+ int[] answer = new int[2];
35+ for(int i=0; i<nums.length; i++) {
36+ for(int j=i+1; j<nums.length; j++) {
37+ if(nums[i]+nums[j]==target) {
38+ answer[0]=i;
39+ answer[1]=j;
40+ return answer;
41+ }
42+ }
43+ }
44+ return answer;
45+ }
46+ */
47+
48+ }
You can’t perform that action at this time.
0 commit comments