Skip to content

[s0ooo0k] WEEK 01 solutions #1709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 26, 2025
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
19 changes: 19 additions & 0 deletions contains-duplicate/s0ooo0k.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 시간복잡도 O(n)
* 공간복잡도 O(n)
*/
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> set = new HashSet<>();

for(int n : nums) {
if(set.contains(n)){
Copy link

Choose a reason for hiding this comment

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

Set을 이용한 제너럴한 문제 풀이를 하시긴 했는데
이렇게 되면 중복이 나올때까지, contains와 add 두개의 함수가 매번 호출되니까,
add를 할 때, 실패하면 한번의 함수로 판단할 수 있습니다.

    public boolean containsDuplicate(int[] nums) {

        Set<Integer> set = new HashSet<>();

        for (int num : nums) {

            boolean isAdded = set.add(num);

            if (!isAdded) {
                return true;
            }
        }

        return false;

    }

Copy link
Member Author

Choose a reason for hiding this comment

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

더 좋은 방법이네요! 감사합니다!

return true;
}
set.add(n);
}
return false;
}
}


48 changes: 48 additions & 0 deletions two-sum/s0ooo0k.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.Map;

class Solution {

/*
* 시간복잡ㄷ도 개선
*
* 시간복잡도 O(n)
* 공간복잡도 O(n)
*/
public int[] twoSum(int[] nums, int target) {
Copy link

Choose a reason for hiding this comment

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

lgtm

Map<Integer, Integer> arr = new HashMap<>();

for(int i=0; i<nums.length; i++) {
if(arr.containsKey(target-nums[i])) {
return new int[]{arr.get(target-nums[i]), i};
}
arr.put(nums[i], i);
}
return new int[]{0, 0};
}



/*
* 기존 풀이
*
* 시간복잡도 O(n^2)
* 공간복잡도 O(1)
*/

/*
public int[] twoSum(int[] nums, int target) {
int[] answer = new int[2];
for(int i=0; i<nums.length; i++) {
for(int j=i+1; j<nums.length; j++) {
if(nums[i]+nums[j]==target) {
answer[0]=i;
answer[1]=j;
return answer;
}
}
}
return answer;
}
*/
}