> 技术文档 > 代码随想录算法训练营第三十二天

代码随想录算法训练营第三十二天

目录

LeetCode.509 斐波那契数

题目链接 斐波那契数

题解

LeetCode.70 爬楼梯

题目链接 爬楼梯

题解

LeetCode.746 使用最小花费爬楼梯

题目链接 使用最小花费爬楼梯

题解


LeetCode.509 斐波那契数

题目链接 斐波那契数

题解

class Solution { public int fib(int n) { int[] f= new int[32]; f[0]= 0; f[1]= 1; if(n < 2){ return n; } for(int i = 2;i<=n;i++){ f[i] = f[i-1] + f[i-2]; } return f[n]; }}

LeetCode.70 爬楼梯

题目链接 爬楼梯

题解

class Solution { public int climbStairs(int n) { int[] f = new int[50]; f[1] = 1; f[2] = 2; if(n <= 2) return n; for(int i = 3;i<=n;i++){ f[i] = f[i-1] + f[i-2]; } return f[n]; }}

LeetCode.746 使用最小花费爬楼梯

题目链接 使用最小花费爬楼梯

题解

class Solution { public int minCostClimbingStairs(int[] cost) { int res = 0; int[] f = new int[1005]; // f[i] 表示爬到 第i个位置的最小花费 f[0] = 0; f[1] = 0; for(int i = 2;i<=cost.length;i++){ f[i] = Math.min(f[i-2] + cost[i-2],f[i-1] + cost[i-1]); } return f[cost.length]; }}

总结

今天的任务非常简单。