509. 斐波那契数
class Solution:def fib(self, n: int) -> int:if n ==1:return 1if n ==0:return 0 dp = [0]*(n+1)dp[1] = 1for i in range(2,n+1):dp[i] = dp[i-1] + dp[i-2]return dp[-1]
70. 爬楼梯
class Solution:def climbStairs(self, n: int) -> int:dp = [0] * (n+1)if n==1:return 1dp[1] = 1dp[2] = 2for i in range(3,n+1):dp[i] = dp[i-1] + dp[i-2]return dp[-1]
746. 使用最小花费爬楼梯
class Solution:def minCostClimbingStairs(self, cost: List[int]) -> int:dp = [0 for i in range(len(cost)+1)]dp[0] = 0dp[1] = 0for i in range(2,len(cost)+1):dp[i] = min(dp[i-1] + cost[i-1],dp[i-2] + cost[i-2])return dp[-1]
62. 不同路径
class Solution:def uniquePaths(self, m: int, n: int) -> int:dp = [[0 for i in range(n)] for j in range(m)]for i in range(m):dp[i][0] = 1for i in range(n):dp[0][i] = 1for i in range(1,m):for j in range(1,n):dp[i][j] = dp[i][j-1] + dp[i-1][j]return dp[-1][-1]
63. 不同路径 II
class Solution:def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:m = len(obstacleGrid)n = len(obstacleGrid[0])dp = [[0 for i in range(n)] for j in range(m)]for i in range(m):if obstacleGrid[i][0] !=1:dp[i][0] = 1else:breakfor i in range(n):if obstacleGrid[0][i] !=1:dp[0][i] = 1else:breakfor i in range(1,m):for j in range(1,n):if obstacleGrid[i][j] !=1:dp[i][j] = dp[i][j-1] + dp[i-1][j]return dp[-1][-1] if obstacleGrid[-1][-1] !=1 else 0
343. 整数拆分
class Solution:def integerBreak(self, n: int) -> int:if n <=2:return 1dp = [0] * (n+1)dp[0] = 0dp[1] = 1dp[2] = 1dp[3] = 2for i in range(1,n+1):for j in range(1,i):dp[i] = max(dp[i], j * (i-j) , j * dp[i-j])return dp[-1]
416. 分割等和子集
class Solution:def canPartition(self, nums: List[int]) -> bool:target = sum(nums)if target % 2 == 1: return Falsetarget //= 2dp = [0] * (target + 1)for i in range(len(nums)):for j in range(target, nums[i] - 1, -1):dp[j] = max(dp[j], dp[j - nums[i]] + nums[i])return target == dp[target]