-
-
Notifications
You must be signed in to change notification settings - Fork 244
[krokerdile] WEEK 02 solutions #1225
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
864182b
f34b332
1691310
2dfdd37
3191122
7d8dc07
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,58 @@ | ||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
nums.sort() | ||
res = [] | ||
n = len(nums) | ||
|
||
for i in range(n): | ||
if i > 0 and nums[i] == nums[i-1]: | ||
continue | ||
|
||
target = -nums[i] | ||
seen = set() | ||
j = i + 1 | ||
|
||
while j < n: | ||
complement = target - nums[j] | ||
if complement in seen: | ||
res.append([nums[i], complement, nums[j]]) | ||
while j + 1 < n and nums[j] == nums[j+1]: | ||
j += 1 | ||
seen.add(nums[j]) | ||
j += 1 | ||
return list(set(tuple(x) for x in res)) | ||
|
||
|
||
class Solution: | ||
def threeSum(self, nums: List[int]) -> List[List[int]]: | ||
nums.sort() | ||
result = [] | ||
|
||
for i in range(len(nums)): | ||
# 중복된 첫 번째 수는 skip | ||
if i > 0 and nums[i] == nums[i - 1]: | ||
continue | ||
|
||
left, right = i + 1, len(nums) - 1 | ||
|
||
while left < right: | ||
total = nums[i] + nums[left] + nums[right] | ||
|
||
if total == 0: | ||
result.append([nums[i], nums[left], nums[right]]) | ||
|
||
# 중복된 두 번째, 세 번째 수 건너뛰기 | ||
while left < right and nums[left] == nums[left + 1]: | ||
left += 1 | ||
while left < right and nums[right] == nums[right - 1]: | ||
right -= 1 | ||
|
||
left += 1 | ||
right -= 1 | ||
|
||
elif total < 0: | ||
left += 1 # 합이 작으면 왼쪽을 오른쪽으로 | ||
else: | ||
right -= 1 # 합이 크면 오른쪽을 왼쪽으로 | ||
|
||
return result | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Solution: | ||
def climbStairs(self, n: int) -> int: | ||
one, two = 1,1 | ||
|
||
for i in range(n-1): | ||
temp = one; | ||
one = one + two; | ||
two = temp; | ||
|
||
return one; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution: | ||
def productExceptSelf(self, nums: List[int]) -> List[int]: | ||
n = len(nums) | ||
answer = [1] * n | ||
|
||
# 1. 왼쪽 곱 저장 | ||
left_product = 1 | ||
for i in range(n): | ||
answer[i] = left_product | ||
left_product *= nums[i] | ||
|
||
# 2. 오른쪽 곱을 곱해주기 | ||
right_product = 1 | ||
for i in reversed(range(n)): | ||
answer[i] *= right_product | ||
right_product *= nums[i] | ||
|
||
return answer |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution: | ||
def isAnagram(self, s: str, t: str) -> bool: | ||
a = {} | ||
b = {} | ||
|
||
if len(s) != len(t): | ||
return False | ||
|
||
for i in range(len(s)): | ||
a[s[i]] = a.get(s[i], 0) + 1 | ||
b[t[i]] = b.get(t[i], 0) + 1 | ||
Comment on lines
+9
to
+11
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. 제가 파이썬을 하지 않아서 이 부분이 잘 이해가 가지 않는데 어떤 순서로 리스트에 넣는 건가요? |
||
|
||
return a == b |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class Solution: | ||
def isValidBST(self, root: Optional[TreeNode]) -> bool: | ||
def validate(node, low, high): | ||
if not node: | ||
return True | ||
|
||
# 현재 노드의 값이 범위를 벗어나면 False | ||
if not (low < node.val < high): | ||
return False | ||
|
||
# 왼쪽 서브트리는 최대값을 현재 노드 값으로 제한 | ||
# 오른쪽 서브트리는 최소값을 현재 노드 값으로 제한 | ||
return validate(node.left, low, node.val) and validate(node.right, node.val, high) | ||
|
||
return validate(root, float('-inf'), float('inf')) | ||
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. 저는 자바로 해서 long을 사용했는데 파이썬에서는 float로 하군요! |
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.
포인터를 사용해서 하셨군요!
여러가지 방법을 배웁니다
저도 이 방법으로 다시 풀어봐야 겠어요
혹시 2가지 방법 중 어떤 것이 속도가 더 빨랐나요?