509. 斐波那契数
class Solution {
public int fib(int n) {
if(n == 0) return 0;
if(n == 1) return 1;
int a = 0;
int b = 1;
int c = 0;
for(int i = 2;i <= n;i++){
c = a + b;
a = b;
b = c;
}
return c;
}
}
70. 爬楼梯
class Solution {
public int climbStairs(int n) {
int a = 1;
int b = 1;
int c = 1;
for(int i = 2;i <= n;i++){
c = a + b;
a = b;
b = c;
}
return c;
}
}
746. 使用最小花费爬楼梯
class Solution {
public int minCostClimbingStairs(int[] cost) {
int a = cost[0];
int b = cost[1];
int c = 0;
for(int i = 2;i < cost.length;i++){
c = Math.min(a,b) + cost[i];
a = b;
b = c;
}
return Math.min(a,b);
}
}