-
-
Notifications
You must be signed in to change notification settings - Fork 195
[wisdom08] Week1 #1132
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
Closed
Closed
[wisdom08] Week1 #1132
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1ac782f
Add solution for leetcode containsDuplicate
wisdom08 d862084
Add solution for leetcode twoSum
wisdom08 e870f9f
Add solution for leetcode twoSum-2
wisdom08 75c9a4a
Add solution for leetcode topKFrequent
wisdom08 8dd7af8
Add solution for leetcode longestConsecutive
wisdom08 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,7 @@ | ||
class Solution(object): | ||
def containsDuplicate(self, nums): | ||
set_nums = set(nums) | ||
if len(nums) == len(set_nums): | ||
return False | ||
else: | ||
return True |
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,10 @@ | ||
def longestConsecutive(nums): | ||
longest = 0 | ||
|
||
for num in nums: | ||
length = 1 | ||
while num + length in nums: | ||
length += 1 | ||
longest = max(longest, length) | ||
|
||
return longest |
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,10 @@ | ||
def topKFrequent(nums, k): | ||
frequency = {} | ||
for num in nums: | ||
frequency[num] = frequency.get(num, 0) + 1 | ||
|
||
sorted_frequency = sorted(frequency, key=lambda num: frequency[num]) | ||
return sorted_frequency[-k:] | ||
|
||
|
||
print(topKFrequent([1, 1, 1, 2, 2, 3], 2)) |
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,9 @@ | ||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
dict = {} | ||
for i, v in enumerate(nums): | ||
diff = target - v | ||
if diff in dict: | ||
return [i, dict[diff]] | ||
else: | ||
dict[v] = i |
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.
여기 if 구문이 dict를 순회하면서 맞는키가 있는지 보는건가로 알고 있었는데 키가 존재하는지만 확인하는 O(1) 메서드였네요..
코드 보면서 의아했는데 정말 깔끔하게 푸신거 보고 하나 배웠습니다!