-
-
Notifications
You must be signed in to change notification settings - Fork 245
[hi-rachel] WEEK 01 solutions #1683
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
""" | ||
https://leetcode.com/problems/contains-duplicate/ | ||
Given an integer array nums, | ||
return true if any value appears at least twice in the array, | ||
and return false if every element is distinct. | ||
TC: O(n) | ||
SC: O(n) | ||
""" | ||
|
||
from typing import List | ||
|
||
class Solution: | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
seen = set() | ||
|
||
for num in nums: | ||
if num in seen: | ||
return True | ||
else: | ||
seen.add(num) | ||
Comment on lines
+21
to
+22
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. 바로 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. @hyunjung-choi 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. @hi-rachel 아 저 뜻은 for num in nums:
if num in seen:
return True
seen.add(num)
return False 이렇게 해도 될 것 같다는 뜻이었습니다. 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. @hyunjung-choi 굳이 else를 쓰지 않아도 된다는 말씀이셨군요. 그렇네요. 감사합니다! |
||
return False | ||
|
||
|
||
class Solution: | ||
def containsDuplicate(self, nums: List[int]) -> bool: | ||
return len(set(nums)) != len(nums) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,17 @@ | ||
""" | ||
처음 풀이 | ||
O(N^2) time, O(N) space | ||
""" | ||
https://leetcode.com/problems/two-sum/ | ||
|
||
# class Solution: | ||
# def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
# result = [] | ||
# for i in range(len(nums)): | ||
# rest = target - nums[i] | ||
# rest_nums = nums[i+1:] | ||
# if rest in rest_nums: | ||
# result.extend([i, rest_nums.index(rest)+i+1]) | ||
# break | ||
# return result | ||
|
||
Given an array of integers nums and an integer target, | ||
return indices of the two numbers such that they add up to target. | ||
You may assume that each input would have exactly one solution, and you may not use the same element twice. | ||
You can return the answer in any order. | ||
|
||
""" | ||
개선 코드 | ||
O(N) time, O(N) space | ||
""" | ||
|
||
from typing import List | ||
|
||
class Solution: | ||
def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
indices = {} | ||
|
@@ -31,6 +23,24 @@ def twoSum(self, nums: List[int], target: int) -> List[int]: | |
return [i, j] | ||
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. O(N)으로 개선하셨네요! 저도 개선해봐야겠어요!👍 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. @hyunjung-choi @printjin-gmailcom https://leetcode.com/problems/two-sum/description/ 문제에 'You can return the answer in any order.'라고 써있어서 그렇게 냈습니다! 리트코드 풀 때 반환을 요구하는 경우도 봤습니다. 그렇다면 [j, i]로 내야겠네요. 리뷰 감사합니다. 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. 리턴할 때 [i, j] 대신 [j, i] 순서가 문제에서 요구하는 경우가 많으니, 문제 조건에 맞게 순서를 한 번 확인해보는 게 좋습니다. |
||
indices[v] = i | ||
|
||
|
||
""" | ||
처음 풀이 | ||
O(N^2) time, O(N) space | ||
""" | ||
|
||
# class Solution: | ||
# def twoSum(self, nums: List[int], target: int) -> List[int]: | ||
# result = [] | ||
# for i in range(len(nums)): | ||
# rest = target - nums[i] | ||
# rest_nums = nums[i+1:] | ||
# if rest in rest_nums: | ||
# result.extend([i, rest_nums.index(rest)+i+1]) | ||
# break | ||
# return result | ||
|
||
|
||
# JS 풀이 | ||
# /** | ||
# * @param {number[]} nums | ||
|
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.
16줄의 seen = set() 방식은 하나씩 돌면서 바로 중복을 체크하는 방식이고,
28줄의 len(set(nums)) != len(nums) 방식은 전체 리스트를 한 번에 변환해 길이로 비교하는 방식입니다.
두 가지 방법을 함께 볼 수 있어서 이해하는 데 많은 분들에게 큰 도움이 될 것 같습니다!