Skip to content

Commit 0fefe2c

Browse files
committed
feat: two sum
1 parent dda994d commit 0fefe2c

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

two-sum/JEONGBEOMKO.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
1-
package week01.twosum;
2-
31
import java.util.HashMap;
42
import java.util.Map;
3+
/*
4+
시간복잡도: O(n)
5+
공간복잡도: O(n)
6+
*/
57

6-
public class JEONGBEOMKO {
7-
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);
13-
}
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);
13+
}
1414

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-
}
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 };
2019
}
21-
22-
return new int[] {};
2320
}
21+
22+
return new int[] {};
2423
}
2524
}

0 commit comments

Comments
 (0)