Skip to content

Commit 80454ac

Browse files
authored
Merge pull request #2379 from Hyeri1ee/main
[Hyeri1ee] WEEK 01 solutions
2 parents 3d95690 + f9957ea commit 80454ac

2 files changed

Lines changed: 38 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.util.*;
2+
3+
class Solution {
4+
Set<Integer> save = new HashSet<>();//O(1)์— ์ด๋ฏธ ๋“ฑ์žฅํ–ˆ๋Š”์ง€ ํŒ๋ณ„ ์œ„ํ•จ
5+
public boolean containsDuplicate(int[] nums) {
6+
7+
for(int i=0; i<nums.length; i++){
8+
if (!save.contains(nums[i])) save.add(nums[i]);
9+
else return true;
10+
}
11+
return false;
12+
}
13+
}
14+

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.*;
2+
//goal : ๋‘๊ฐœ์˜ ์ธ๋ฑ์Šค ๋ฐ˜ํ™˜
3+
class Solution {
4+
static int[] answer = new int[2];
5+
public int[] twoSum(int[] nums, int target) {
6+
Map<Integer, Integer> maps = new HashMap<>();//num , index
7+
for(int i = 0; i < nums.length; i++){
8+
int t = target - nums[i]; //์ƒˆ๋กœ์šด ํƒ€๊ฒŸ
9+
//O(1)์œผ๋กœ ๋‹ค์Œ ๋ถ€๋ถ„ ์ฐพ๊ธฐ
10+
if (maps.containsKey(t)){
11+
answer[0] = i;
12+
answer[1] = maps.get(t);
13+
14+
Arrays.sort(answer);
15+
return answer;
16+
}
17+
18+
maps.put(nums[i], i);
19+
}
20+
21+
return answer;
22+
23+
}
24+
}

0 commit comments

Comments
ย (0)