有这样一道OJ
You are climbing a stair case. It takes n steps to reach to the top.
Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?
https://oj.leetcode.com/problems/climbing-stairs/
咋一看不就是简单的斐波那契数列吗,递归就行了。
但是递归的话超时了。
于是用了动态规划
public class Solution {
public int climbStairs(int n) {
if(n==1) return 1;
if(n==2) return 2;
int pre = 1;
int last = 2;
for(int i=3; i<n; i++){
int tmp = last;
last += pre;
pre = tmp;
}
return last + pre;
}
}