递归方法
class Solution {
/* private Map<Integer,Integer> storeMap = new HashMap<>();
public int climbStairs(int n) {
if(n == 1) return 1;
if(n == 2) return 2;
if(null != storeMap.get(n))
return storeMap.get(n);
else{
int result = climbStairs(n-1)+climbStairs(n-2);
storeMap.put(n,result);
return result;
}
}
*/
循环方法
public int climbStairs(int n){
if(n == 1) return 1;
if(n == 2) return 2;
int result = 0;
int pre = 2;
int prePre = 1;
for (int i = 3;i <=n; ++i) {
result = pre + prePre;
prePre = pre;
pre = result;
}
return result;
}
}