您的位置:首页 > 大数据 > 人工智能

Climbing Stairs - Leetcode

2015-02-12 13:45 253 查看
Iterrative 实现递归

public class Solution {
public int climbStairs(int n) {
int prev=0, cur=1;
for(int i=1; i<=n; i++){
int tmp=cur;
cur += prev;
prev=tmp;
}
return cur;
}
}


DP实现:

public class Solution {
public int climbStairs(int n) {
int[] f=new int[n+1];
int[] fun=new int[n+1];
for(int i=0; i<=n; i++){
f[i]=i;
}

for(int i=3; i<=n; i++){
f[i]=f[i-1]+f[i-2];
}
return f
;
}
}


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?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: