File tree Expand file tree Collapse file tree 1 file changed +7
-3
lines changed Expand file tree Collapse file tree 1 file changed +7
-3
lines changed Original file line number Diff line number Diff line change 5
5
* 본래 brute force로 이중 for문으로 풀었다가 map으로 최적화.
6
6
*/
7
7
class Solution {
8
+
8
9
/**
9
10
* brute force 풀이
10
11
*/
11
12
// public int[] twoSumByBruteForce(int[] nums, int target) {
12
13
// for (int i = 0; i < nums.length; i++) {
13
- // for (int j = i+ 1; j < nums.length; j++) {
14
+ // for (int j = i + 1; j < nums.length; j++) {
14
15
// if (nums[i] + nums[j] == target) {
15
- // return new int[] { i, j };
16
+ // return new int[]{ i, j};
16
17
// }
17
18
// }
18
19
// }
19
20
// return new int[2];
20
21
// }
21
22
public int [] twoSum (int [] nums , int target ) {
22
23
Map <Integer , Integer > numberMap = new HashMap <>();
24
+
23
25
for (int i = 0 ; i < nums .length ; i ++) {
24
26
int required = target - nums [i ];
25
27
Integer index = numberMap .get (required );
26
28
27
29
if (index != null ) {
28
30
return new int []{index , i };
29
31
}
32
+
30
33
numberMap .put (nums [i ], i );
31
34
}
35
+
32
36
return new int [2 ];
33
37
}
34
- }
38
+ }
You can’t perform that action at this time.
0 commit comments