Skip to content

Commit dda994d

Browse files
committed
WEEK 01 solutions
Two sum solution
1 parent 0024346 commit dda994d

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

โ€Žtwo-sum/JEONGBEOMKO.javaโ€Ž

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1+
package week01.twosum;
2+
13
import java.util.HashMap;
24
import java.util.Map;
35

4-
class Solution {
5-
public int[] twoSum(int[] nums, int target) {
6-
Map<Integer, Integer> numberMap = new HashMap<>();
7-
for (int i=0; i<nums.length; i++) {
8-
numberMap.put(nums[i], i);
9-
}
6+
public class JEONGBEOMKO {
107

11-
for(int i=0; i<nums.length; i++) {
12-
int operand = target - nums[i];
13-
if (numberMap.containsKey(operand) && numberMap.get(operand) != i) { // ์ž๊ธฐ ์ž์‹ ์€ ์ œ์™ธ
14-
return new int[] { numberMap.get(target - nums[i]), i };
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);
1513
}
16-
}
1714

18-
return new int[] {};
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+
}
20+
}
21+
22+
return new int[] {};
23+
}
1924
}
2025
}

0 commit comments

Comments
ย (0)