Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions contains-duplicate/hyeri1ee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import java.util.*;

class Solution {
Set<Integer> save = new HashSet<>();//O(1)에 이미 등장했는지 판별 위함
public boolean containsDuplicate(int[] nums) {

for(int i=0; i<nums.length; i++){
if (!save.contains(nums[i])) save.add(nums[i]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서는 Set.add의 반환값을 활용해서

if (!save.add(nums[i])) return true;

처럼 한 줄로 줄여볼 수도 있을 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set의 add를 통해 한 줄로 줄여볼 수도 있네요! 조언 감사합니다

else return true;
}
return false;
}
}

24 changes: 24 additions & 0 deletions two-sum/Hyeri1ee.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.*;
//goal : 두개의 인덱스 반환
class Solution {
static int[] answer = new int[2];
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> maps = new HashMap<>();//num , index
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

HashMap을 사용해서 {값 -> 인덱스}를 저장해 두고 한 번의 순회로 정답을 찾는 방식이라 시간 복잡도가 O(n)으로 잘 설계된 풀이로 보입니다!
특히 int t = target - nums[i];로 필요한 값을 계산해 두고 containsKey로 바로 확인하는 흐름이 깔끔하고 좋은 것 같습니다! 👍

for(int i = 0; i < nums.length; i++){
int t = target - nums[i]; //새로운 타겟
//O(1)으로 다음 부분 찾기
if (maps.containsKey(t)){
answer[0] = i;
answer[1] = maps.get(t);

Arrays.sort(answer);
return answer;
}

maps.put(nums[i], i);
}

return answer;

}
}