Climbing Stairs - Leetcode
2015-02-12 13:45
253 查看
Iterrative 实现递归
DP实现:
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?
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?
相关文章推荐
- leetcode70-Climbing Stairs(斐波那契数列)
- leetcode - Climbing Stairs
- [LeetCode] Climbing Stairs
- [leetcode]Climbing Stairs
- [LeetCode][Java] Climbing Stairs
- LeetCode -- Climbing Stairs
- 65 leetcode - Climbing Stairs
- (java)leetcode-70:Climbing Stairs
- 【LeetCode】Climbing Stairs
- 【LeetCode 70】Climbing Stairs(Python)
- LeetCode——Climbing Stairs
- leetcode-Climbing Stairs
- [leetcode]Climbing Stairs
- [leetcode]Climbing Stairs
- 【LeetCode】70 - Climbing Stairs
- LeetCode - Climbing Stairs
- leetcode 070 —— Climbing Stairs
- LEETCODE: Climbing Stairs
- LeetCode——Climbing Stairs
- leetcode 70 Climbing Stairs