0283. Move Zeroes / 0344. Reverse String
class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
length=len(nums)
if length != 1:
first_encountered = False
for x in range(length-1):
if nums[x] == 0:
if not first_encountered:
first_encountered = True
y = x + 1
while y<length:
if nums[y] != 0:
nums[x],nums[y] = nums[y],nums[x]
break
y+=1
class Solution:
def reverseString(self, s: List[str]) -> None:
"""
Do not return anything, modify s in-place instead.
"""
x,y=0,len(s)-1
while x<y:
s[x],s[y]=s[y],s[x]
x+=1
y-=1
return s
๐ 0283) ํ ์ชฝ์์ ๋์์ ์์ํ๋ ํฌ ํฌ์ธํฐ ์ ํ์ผ๋ก, ํฌ ํฌ์ธํฐ x๋ 0์, y๋ 0์ด ์๋ ์ชฝ์ ๊ฐ๋ฆฌํค๋ฉด์ ์์. x๋ 0์ ๊ฐ๋ฆฌํฌ ๋๋ง(for๋ฌธ ๋๋ฉด์), y๋ (x+1)๋ถํฐ ์์ํด 0์ด ์๋ ๊ณณ์ ์ฐพ๋๋ค๋ฉด ๋ฐ๋ก swap. ํฌ ํฌ์ธํฐ ์ ํ์ x์ y ๋ชจ๋ ๊ฐ๊ฐ ์ ์ฒด ๋ฒ์ ๋ฑ ํ ๋ฒ์ฉ๋ง traverseํ๋ฏ๋ก, ํ ๋ฒ x๊ฐ 0์ ๊ฐ๋ฆฌํฌ ๋ y๋ฅผ ์ฒ์์ ๋ฑ x + 1์ด๋ผ ์ค์ ํ๊ณ . ๊ทธ ์ดํ๋ y๋ฅผ ์ ์ฒด ๋ฒ์๊น์ง ์ญ ํ๋ฒ๋ง ๋๋ฆฌ๊ฒ ํ๋ฉด ๋๋ค(first_encountered flag ๋ณ์ ํ์ฉ)
๐ 0344) ๋ ํฌ์ธํฐ(x,y)๋ฅผ ์์ชฝ ๋์ ์ก๊ณ ๊ฐ์ด๋ฐ๋ก ํฅํ๋ฉด์ s[x], s[y] = s[y], s[x] swap ์งํ. x์ y ํฉ์ณ์ ํ๋ฐํด๋ง ์งํํ๋ฏ๋ก O(N)
0917. Reverse Only Letters / 0674. Longest Continuous Increasing Subsequence
class Solution:
def reverseOnlyLetters(self, s: str) -> str:
s=list(s)
x,y=0,len(s)-1
while x<y:
if s[x].isalpha():
if s[y].isalpha():
s[x],s[y]=s[y],s[x]
x+=1
y-=1
else:
y-=1
else:
x+=1
return ''.join(s)
class Solution:
def findLengthOfLCIS(self, nums: List[int]) -> int:
dp=[1]*len(nums)
for x in range(1,len(nums)):
if nums[x-1] < nums[x]:
dp[x] = dp[x-1] + 1
return max(dp)
๐ 0917) ์ 344์ ๋์ผ ์ ํ. ๋จ, ์ํ๋ฒณ์ผ ๊ฒฝ์ฐ๋ง swap ํด์ฃผ๋ฉด ๋๋ค. ์ธ์ ํฌ์ธํฐ๋ฅผ ์ ๋ฐ์ดํธ ํด์ค ์ง ๊ฒฝ์ฐ์ ์ ํ์ธํ๋ฉด ๋๋ค.
๐ 0674) dp ํ์ฉ. LIS์ '์ฐ์ํ' subarray ๋ฒ์ . (i-1) ์ธ๋ฑ์ค ์์์ (i) ์ธ๋ฑ์ค ์์๊ฐ ์๋ค๋ฉด, i-1์ด ๊ฐ๋ฆฌํค๋ ์์๋ณด๋ค i๊ฐ ๊ฐ๋ฆฌํค๋ ์์๊ฐ strictly increasing(<)ํ๋ค๋ฉด i-1์ด ๊ฐ๋ฆฌํค๋ dp ํ ์ด๋ธ์ ๊ฐ๋ณด๋ค 1์ ์ถ๊ฐ(i์ธ๋ฑ์ค ์์ 1๊ฐ๊ฐ ๋ถ์ผ๋ฏ๋ก)
0226. Invert Binary Tree / 0110. Balanced Binary Tree
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
stack=[root]
while stack:
node = stack.pop()
if not node:
continue
node.left, node.right = node.right, node.left
stack += [node.left, node.right]
return root
#----------------------------------------------------------------------------------
class Solution:
def invertTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
if not root:
return None
left,right = root.left, root.right
root.left = self.invertTree(right)
root.right = self.invertTree(left)
return root
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isBalanced(self, root: Optional[TreeNode]) -> bool:
def get_depth_of_a_tree(root):
if not root: return 0
left, right = root.left, root.right
return max(get_depth_of_a_tree(left), get_depth_of_a_tree(right)) + 1
def dfs(tree):
if not tree:
l_h, r_h = 0,0
return
l_h, r_h = get_depth_of_a_tree(tree.left), get_depth_of_a_tree(tree.right)
if abs(l_h-r_h) > 1:
return -1
left, right = tree.left, tree.right
dl, dr = dfs(left), dfs(right)
if dl == -1 or dr == -1:
return - 1
return dfs(root) != -1
๐ 0226) root๋ผ๋ tree ์ ์ฒด ํด๋์ค๋ก ์ฃผ์ด์ก์ ๋, ์ฌ๊ท์ ์ผ๋ก ์ผ์ชฝ subtree์ ์ค๋ฅธ์ชฝ subtree๋ฅผ ๋ฐ๊พธ๋ฉด ๋๋ค. ํ์ด์ฌ์ swap ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ๋งค์ฐ ์ฝ๊ฒ node์ left์ node์ right๋ฅผ ๋ฐ๊พธ๋ฉด ๋๋ค. ๋ง์ฝ tree์ depth๊ฐ ๋งค์ฐ ๊น์ด์ง ๊ฒฝ์ฐ ์ฌ๊ท ์ค๋ฅ ๊ฐ๋ฅ์ฑ(์๋ ์๋ฃจ์ (2)). ์ด๋ด ๊ฒฝ์ฐ stack์ ํ์ฉํด ๋จผ์ tree ์ ์ฒด ํด๋์ค๋ฅผ ๋ฃ๊ณ , ๋ฃ์ ํด๋์ค์ ์ผ์ชฝ subtree์ ์ค๋ฅธ์ชฝ subtree๋ฅผ swapํ ๋ค์ ์ฐจ๋ก๋๋ก stack์ ๋ฃ๋๋ค. ๊ทธ ๋ค์ left์ right subtree ๊ฐ๊ฐ ๋ค์ ๋นผ์ ์กด์ฌํ๋ node๋ผ๋ฉด ๋ค์ ํด๋น node์ left์ right swapํ๊ณ stack์ ๋ค์ ๋ฃ๋๋ค. ์ฌ๊ท๋ฅผ stack์ผ๋ก ํํํ ์ ์๋ ์ด์ ๋, ์ต๊ทผ iteration ์งํํ ๋ถ๋ถ์์ ๋ค์ iteration ๊น๊ฒ ์งํํ๋ฏ๋ก stack ์๋ฃ๊ตฌ์กฐ ํน์ฑ ์ ์ต๊ทผ์ ๋ฃ์ ๋ฐ์ดํฐ๋ฅผ ๊บผ๋ธ ๋ค๋ ์ฑ์ง์ด ๋์ผ.
๐ 0110) Binary Tree๊ฐ Balanced ๋์๋ค๋ ๊ฑด tree๋ด์ ๋ชจ๋ sub-tree๊ฐ root ์ ์ธ left์ right์ depth ์ฐจ์ด๊ฐ 1๋ณด๋ค ํฌ์ง ์๋ค๋ ๋ป์ด๋ค. ์ฆ ์ฌ๊ท์ ์ผ๋ก, ์ ์ฒด root๋ถํฐ ์์ํด left์ right sub-tree์ depth ์ฐจ์ด๊ฐ 1๋ณด๋ค ํฌ์ง ์์ ์ง ๊ณ์ ํ์ธ. ์ฐจ์ด๊ฐ 1๋ณด๋ค ํฌ๋ค๋ฉด ๋ฐ๋ก -1 return. dfs(left)์ dfs(right)์ ๊ฒฐ๊ณผ ์ค ์ต์ 1๊ฐ๊ฐ -1์ด๋ฉด ์ ์ฒด unbalanced์ด๋ฏ๋ก -1 return.
0104. Maximum Depth of Binary Tree / 0100. Same Tree
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def maxDepth(self, root: Optional[TreeNode]) -> int:
if root is None:
return 0
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool:
if not p and not q:
return True
if not p or not q or p.val != q.val:
return False
if p.val == q.val:
return (self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right))
๐ 0104) ์ฃผ์ด์ง Binary Tree์ ์ต๋ ๊น์ด๋ ์ฌ๊ทํจ์๋ก ํด๊ฒฐํ ์ ์๋ค. ์ฃผ์ด์ง root tree์ ์ผ์ชฝ๊ณผ ์ค๋ฅธ์ชฝ subtree ๊ฐ๊ฐ์ ์ต๋๊ฐ depth ์ค max() ๊ฒฐ๊ณผ์ 1์ ๋ํ ๊ฒฐ๊ณผ๊ฐ ์ ์ฒด root tree์ ๊น์ด(root ํฌํจํ๋ฏ๋ก 1 ๋ํด์ผ ํ๋ค)
๐ 0100) ์ฌ๊ทํจ์๋ฅผ ํ์ฉํ์ฌ left child์ right child ๊ฐ๊ฐ p์ q๋ก not p and not q์ผ ๋๊น์ง ์ฌ๊ท ๋๋ฆฌ๋ฉด ๋๋ค. ํ์ฌ node์ val์ด ์๋ก ๊ฐ๋ค๋ฉด ๊ทธ ๋ค์ child (p.left / q.left) (p.right / q.right) ํ์ธ
0876. Middle of the Linked List / 0125. Valid Palindrome
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def middleNode(self, head: Optional[ListNode]) -> Optional[ListNode]:
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
class Solution:
def isPalindrome(self, s: str) -> bool:
l = []
for x in s:
if x.isdigit():
l.append(x)
elif x.isalpha():
l.append(x.lower())
if l == l[::-1]:
return True
else:
return False
#
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
๐ 0876) Linked List ๋ฌธ์ ํ์ด์์ ๊ฐ์ฅ ๋ง์ด ์ฌ์ฉ๋๋ ๊ธฐ๋ฒ fast pointer / slow pointer๋ฅผ ๋์์ ์ฌ์ฉํ๋ฉด ์ฝ๋ค. fast pointer๊ฐ ์ฃผ์ด์ง linked list์ ๋งจ ์ค๋ฅธ์ชฝ ๋ ๋ ธ๋์ ๋๋ฌ(ํ์ ๊ฐ์ ๋ ธ๋)ํ๊ฑฐ๋ ์ค๋ฅธ์ชฝ ๋์ ๊ทธ ๋ค์ None(์ง์ ๊ฐ์ ๋ ธ๋)์ ๋๋ฌํ ๋ slow pointer๊ฐ ๋๋ฌํ ๊ณณ์ด ๋ฌธ์ ์์ ์๊ตฌํ๋ middle of the linked list.
๐ 0125) ์ซ์์ ์ํ๋ฒณ๋ง ๋ชจ๋ ๋ชจ์ ๋ค์(์ํ๋ฒณ์ด๋ฉด ์๋ฌธ์๋ก ๋ฐ๊พธ๊ณ ๋ชจ์ผ๊ธฐ) ๋ฐ๋ก ํฐ๋ฆฐ๋๋กฌ ํ์ . ํ์ ์ [::-1]๊ณผ ๋น๊ตํ๊ฑฐ๋ ์ง์ ์ ์์ ๋ ๊ฐ์ ํฌ์ธํฐ๋ฅผ ๋๊ณ traverseํ๋ฉด์ ๋น๊ตํด๋ ๋๋ค
0206. Reverse Linked List / 0141. Linked List Cycle
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def reverseList(self, head: Optional[ListNode]) -> Optional[ListNode]:
prev, node = None, head
while node is not None:
tmp = node.next
node.next = prev
prev = node
node = tmp
return prev
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def hasCycle(self, head: Optional[ListNode]) -> bool:
slow, fast = head, head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if slow == fast:
return True
return False
๐ 0206) reverse ๊ณผ์ ์ ์งํํ๊ธฐ ์ํด์๋ ํ์ฌ ๋ ธ๋์ ๋ฐ๋ก ์์ ๋ ธ๋ ๊ฐ๊ฐ node / prev๋ผ๊ณ ํ ๋ค์, node.next = prev๋ก ๊ฐ๋ฆฌํค๊ฒ ํ๋ฉด ๋๋ค. ์ด ๋, node.next๊ฐ prev๋ก ๊ฐ๋ฆฌํค๋ ์๊ฐ, node ์ค๋ฅธ์ชฝ์ผ๋ก ์ฐ๊ฒฐ๋ node์์ ์ค์ด ๋์ด์ง๋ฏ๋ก, ์์ tmp ๋ ธ๋๋ก ๋ง๋ค์ด์ผ ํ๋๊ฒ ํต์ฌ
๐ 0141) ์ฃผ์ด์ง linked list๊ฐ cycle์ด ์๋ ์ง ํ๋ณํ๋ ์ฝ๋ ์์ฑํ๊ธฐ. ๋งจ ๋ง์ง๋ง node๊ฐ ๊ทธ ์์ ์ฌ๋ฌ node ์ค ์ฐ๊ฒฐ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค๋ฉด cycle ์กด์ฌ. fast / slow๋ก ๋๋์ด์ fast๊ฐ ์ธ์ ๊ฐ slow๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋๋ค.
0234. Palindrome Linked List / 0643. Maximum Average Subarray I
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def isPalindrome(self, head: Optional[ListNode]) -> bool:
rev = None
slow, fast = head, head
while fast and fast.next:
fast = fast.next.next
rev, rev.next, slow = slow, rev, slow.next
if fast:
slow = slow.next
while rev and rev.val == slow.val:
slow = slow.next
rev = rev.next
return not rev
class Solution:
def findMaxAverage(self, nums: List[int], k: int) -> float:
avg = sum(nums[:k]) / k
avg_sum = sum(nums[:k])
start=0
for i in range(k,len(nums)):
avg_sum-=(nums[start])
avg_sum+=(nums[i])
avg = max(avg,avg_sum/k)
start+=1
return avg
๐ 0234) ๋ฌธ์ ์ ํต์ฌ ์์ด๋์ด๋ ๋ ๋ฐ๋(fast, slow)๋ฅผ ๋์์ head๋ถํฐ ์งํํ๋ฉฐ, fast๊ฐ ๋๊น์ง ๋ค๋ค๋ฅผ ๋๊น์ง slow๋ ์ ๋ฐ์ ์งํํ๊ณ , ์ ๋ฐ๊น์ง ์งํํ๋ ๋์์ ๋ชจ๋ node๋ฅผ rev๋ก ์ ์ฅ / ๊ทธ ๋ค์ ์ค๊ฐ ์ง์ ๋ถํฐ slow์ rev๊ฐ ๊ฐ๊ฐ ๋ค๋ฅธ ๋ฐฉํฅ์ผ๋ก ๋ป์ด๋๊ฐ๋ฉฐ ์ฐจ๋ก๋๋ก slow์ rev์ val์ ๋น๊ต. ์ต์ข ์ ์ผ๋ก rev๊ฐ ์ผ๋ถ ์๋ค๋ ๊ฑด ํฐ๋ฆฐ๋๋กฌ์ด ์๋๋ฏ๋ก False, rev๊ฐ ๋น์๋ค๋ ๊ฑด True์ด๋ฏ๋ก not rev๋ฅผ ๋ฆฌํด
โป ๋จ, ๋ ธ๋ ๊ฐ์๊ฐ ํ์์ผ ๋, rev๋ ์ ํํ๊ฒ ์ค๊ฐ ๋ ธ๋ ๋ฐ๋ก ์ ๋ ธ๋๋ฅผ ์์์ ์ผ๋ก ๊ฐ์ง์ง๋ง, slow๋ ์ ํํ๊ฒ ์ค๊ฐ ๋ ธ๋๋ถํฐ ๊ฐ๋ฆฌํจ๋ค. ๋ฐ๋ผ์, slow๋ ์ค๊ฐ ๋ ธ๋ ๋ฐ๋ก ๋ท ๋ ธ๋๋ฅผ ๊ฐ๋ฆฌํค๊ฒ๋ slow = slow.next๋ก ์ ๋ฐ์ดํธ
ex) ์๋ ์์) node ๊ฐ์๊ฐ ์ง์์ผ๋ ์ ํํ๊ฒ rev / slow ์คํํ ์ง์ ์ค๊ฐ ๊ธฐ์ค์ผ๋ก ๋๋์ง๋ง, ํ์ ๊ฐ์์ผ ๋๋ slow๋ฅผ ์ค๋ฅธ์ชฝ์ผ๋ก ํ ์นธ ์ด๋ํด์ slow = slow.next๋ก ์ ๋ฐ์ดํธ
๐ 0643) ์ ํ์ ์ธ ์ฌ๋ผ์ด๋ฉ ์๋์ฐ ๋ฌธ์ . ์ฐ์ํ, ์ ํด์ง k๊ฐ์ ๊ฐ์์ ํ๊ท ์ค ์ต๋๊ฐ์ ๊ตฌํ๋ ๋ฌธ์ . ์๊ฐ ๋ณต์ก๋ O(N)๋ง์ ํฌ๊ธฐ ๊ณ ์ ํ ์ฑ๋ก ๋งจ ์๊ณผ ๋งจ ๋ค๋ง ์ผ์ดํด์ ๋ฆฌ์คํธ ์ค๋ฅธ์ชฝ ๋๊น์ง ์ฌ๋ผ์ด๋ ํด์ฃผ๋ฉด ๋
0112. Path Sum / 0804. Unique Morse Code Words
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def hasPathSum(self, root: Optional[TreeNode], targetSum: int) -> bool:
def dfs(node, curSum):
if not node: #empty tree
return False
curSum += node.val
if not node.left and not node.right: #if this is a leaf node
return curSum == targetSum
#if not a leaf node
return (dfs(node.left, curSum) or dfs(node.right, curSum))
return dfs(root, 0)
๐ 0112) ์ฃผ์ด์ง binary tree์์ root๋ถํฐ leaf node๊น์ง ์งํํ๋ ๊ฒฝ๋ก ๋ด์ ๋ชจ๋ ๋ ธ๋์ ์ซ์์ ํฉ์ด targetSum์ด ์กด์ฌํ๋ ์ง์ ์ฌ๋ถ๋ฅผ ํ์ธํ๋ ๋ฌธ์ . dfs ์ฌ๊ท ํ์ฉ.
โ ๋จผ์ return ์กฐ๊ฑด) ์ฃผ์ด์ง node๊ฐ ์กด์ฌํ์ง ์์ผ๋ฉด return
โก ์ฃผ์ด์ง node๊ฐ ์กด์ฌํ๋ค๋ฉด ํ์ฌ์ ๊ฐ curSum์์ value ๋ํจ. ๊ทธ๋ฆฌ๊ณ ํด๋น node๊ฐ leaf node๋ผ๋ฉด curSum๊ณผ targetSum์ด ๊ฐ์ ์ง ๋ค๋ฅธ ์ง ๋ฆฌํด. ๋ง์ฝ leaf node๊ฐ ์๋๋ผ๋ฉด dfs ์ฌ๊ท์ ์ผ๋ก ๋๋ฆฌ๋ฉด ๋๋ค.
class Solution:
def uniqueMorseRepresentations(self, words: List[str]) -> int:
hashmap = dict()
values = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
keys = list('abcdefghijklmnopqrstuvwxyz')
for key, value in zip(keys, values):
hashmap[key] = value
ans = set()
for word in words:
ans_str = ''
for alphabet in word:
ans_str += hashmap[alphabet]
ans.add(ans_str)
return len(ans)
๐ 0804) a~z์ ๋์ํ๋ morse code: hash map ํ์ฉ. ์ง์ for๋ฌธ ์ผ์ผ์ด ๋๋ฆฌ๋ฉฐ set() ์๋ฃ๊ตฌ์กฐ ํ์ฉํด ์ค๋ณต ์ฌ๋ถ ํ์ธํ๊ณ ๊ธธ์ด ์ถ๋ ฅ
0771. Jewels and Stones / 0706. Design HashMap
class Solution:
def numJewelsInStones(self, jewels: str, stones: str) -> int:
ans=0
for stone in stones:
if stone in jewels:
ans+=1
return ans
class MyHashMap:
def __init__(self):
self.hashmap = dict()
def put(self, key: int, value: int) -> None:
self.hashmap[key] = value
def get(self, key: int) -> int:
if key in self.hashmap.keys():
return self.hashmap[key]
else:
return -1
def remove(self, key: int) -> None:
if key in self.hashmap.keys():
del self.hashmap[key]
# Your MyHashMap object will be instantiated and called as such:
# obj = MyHashMap()
# obj.put(key,value)
# param_2 = obj.get(key)
# obj.remove(key)
0290. Word Pattern / 0509. Fibonacci Number
class Solution:
def wordPattern(self, pattern: str, s: str) -> bool:
match_dict = dict()
s = s.split()
print(s)
if len(pattern) != len(s):
return False
for i in range(len(pattern)):
# print(match_dict)
if pattern[i] in match_dict.keys():
if match_dict[pattern[i]] != s[i]:
return False
else:
if s[i] in match_dict.values():
return False
match_dict[pattern[i]] = s[i]
return True
๐ 0290) ์ผ๋์ผ ๋์์ด ๋๋ ์ง ์ฌ๋ถ ํ์ธ. X -> Y์์ X์ ๊ธธ์ด์ Y์ ๊ธธ์ด๊ฐ ๋ค๋ฅด๊ฑฐ๋ / X์ ์กด์ฌํ๋๋ฐ Y ๋ฐ์ดํฐ๊ฐ ์๋ก ๋ค๋ฅธ ๊ฒฝ์ฐ / Y์ ์กด์ฌํ๋ ๋ฐ X ๋ฐ์ดํฐ๊ฐ ์๋ก ๋ค๋ฅธ ๊ฒฝ์ฐ False / ๋๋จธ์ง๋ True
class Solution:
def fib(self, n: int) -> int:
def fibo(n):
if n <= 1:
return n
return fibo(n-1) + fibo(n-2)
return fibo(n)
'LeetCode Problems > Easy' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ LeetCode Easy Collections III - 1 Problems (0) | 2025.01.29 |
---|---|
๐ LeetCode Easy Collections I - 20 Problems (1) | 2024.08.05 |
๋๊ธ