-
-
Notifications
You must be signed in to change notification settings - Fork 246
[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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)){ | ||
return true; | ||
} | ||
set.add(n); | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
*/ | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set을 이용한 제너럴한 문제 풀이를 하시긴 했는데
이렇게 되면 중복이 나올때까지, contains와 add 두개의 함수가 매번 호출되니까,
add를 할 때, 실패하면 한번의 함수로 판단할 수 있습니다.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
더 좋은 방법이네요! 감사합니다!