728x90
28ms
class Solution:
def climbStairs(self, n: int) -> int:
if n == 1:
return 1
elif n == 2:
return 2
else:
dp = [0]*(n+1)
dp[1] = 1
dp[2] = 2
for i in range(3, n+1):
dp[i] = dp[i-1] + dp[i-2]
return dp[n]
swap 사용
32 ms
if n == 1:
return 1
a, b = 1, 2
for i in range(2, n):
a, b = b, a + b
return b
728x90
'Leetcode' 카테고리의 다른 글
[LeetCode] Merge Sorted Array 파이썬 (modify in-place) (0) | 2021.03.31 |
---|---|
[LeetCode] Remove Duplicates from Sorted List 파이썬 (Linked List, shallow copy, deepcopy) (0) | 2021.03.31 |
[LeetCode] Add Binary 파이썬 (int, bin) (0) | 2021.03.30 |
[LeetCode] Plus One 파이썬 (join, list comprehension) (0) | 2021.03.30 |
[LeetCode] Length of Last Word 파이썬 (rstrip, split) (0) | 2021.03.30 |
댓글