首页 > 其他分享 >70. Climbing Stairs刷题笔记

70. Climbing Stairs刷题笔记

时间:2023-05-26 22:07:07浏览次数:56  
标签:return int self Climbing Stairs dp 刷题


问题描述 该题考察的是动态规划

class Solution:
    def climbStairs(self, n: int) -> int:
        dp = [0]*n
        if n == 1:
            return 1
        dp[0] = 1
        dp[1] = 2
        for i in range(2,n):
            dp[i] = dp[i-1] + dp[i-2]
        return dp[n-1]

运行结果:

70. Climbing Stairs刷题笔记_leetcode


标签:return,int,self,Climbing,Stairs,dp,刷题
From: https://blog.51cto.com/u_16131692/6359351

相关文章

  • 107. Binary Tree Level Order Traversal II刷题笔记
    问题描述自底向上层序搜索python代码#Definitionforabinarytreenode.#classTreeNode:#def__init__(self,val=0,left=None,right=None):#self.val=val#self.left=left#self.right=rightclassSolution:deflevelOrd......
  • 102. Binary Tree Level Order Traversal刷题笔记
    考察二叉树的层序遍历问题描述leetcode代码:#Definitionforabinarytreenode.#classTreeNode:#def__init__(self,val=0,left=None,right=None):#self.val=val#self.left=left#self.right=rightclassSolution:deflev......
  • 94. Binary Tree Inorder Traversal刷题笔记
    问题描述中序遍历,用的是递归法,当然也可以用迭代法(栈)代码#Definitionforabinarytreenode.#classTreeNode:#def__init__(self,val=0,left=None,right=None):#self.val=val#self.left=left#self.right=rightclassSolution......
  • 145. Binary Tree Postorder Traversal刷题笔记
    问题描述后序遍历代码:#Definitionforabinarytreenode.#classTreeNode:#def__init__(self,val=0,left=None,right=None):#self.val=val#self.left=left#self.right=rightclassSolution:defpostorderTraversal(sel......
  • 144. Binary Tree Preorder Traversal刷题笔记
    问题描述前序遍历。注意嵌套函数里的list应该用append而不是+=来添加元素#Definitionforabinarytreenode.#classTreeNode:#def__init__(self,val=0,left=None,right=None):#self.val=val#self.left=left#self.right=right......
  • 538. Convert BST to Greater Tree刷题笔记
    问题描述第一次没做出来,主要问题还是出在递归上pycharm代码classNode(object):def__init__(self,x):self.val=xself.left=Noneself.right=None#树生成代码defgenerate_tree(vals):iflen(vals)==0:returnNone......
  • 111. Minimum Depth of Binary Tree刷题笔记
    需要考虑这种情况:#Definitionforabinarytreenode.#classTreeNode:#def__init__(self,val=0,left=None,right=None):#self.val=val#self.left=left#self.right=rightclassSolution:defminDepth(self,root:Optiona......
  • 128. Longest Consecutive Sequence刷题笔记
    取巧用了python自带的排序算法,该算法为Timsort,复杂度为nlog(n)classSolution:deflongestConsecutive(self,nums:List[int])->int:ifnotnums:return0nums.sort()res=0length=1foriinrange(len(nu......
  • 120. Triangle刷题笔记
    用DP可以做完classSolution:defminimumTotal(self,triangle:List[List[int]])->int:dp=[0]*(len(triangle)+1)forrowintriangle[::-1]:foriinrange(len(row)):dp[i]=row[i]+min(dp[i],dp[i+1])ret......
  • 1480. Running Sum of 1d Array刷题笔记
    简单难度的题classSolution:defrunningSum(self,nums:List[int])->List[int]:foriinrange(1,len(nums)):nums[i]+=nums[i-1]returnnums......