LeetCode OJ算法题(七十):Climbing Stairs
来源:互联网 发布:苹果mac在哪里升级 编辑:程序博客网 时间:2024/05/16 10:47
题目:
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?
解法:采用DP算法,找到递推关系式。
假设到第n个台阶有f(n)种走法,那么f(n) = f(n-1)+f(n-2),因为前一步不是在第n-1个台阶上,就是在第n-2个台阶上。用一张n*1的表存储f(n),注意n=1和n=2时的初始值分别为1和2,。
public class No70_ClimbingStairs {public static void main(String[] args){System.out.println(climbStairs(5));//f(n) = f(n-1)+ f(n-2);}public static int climbStairs(int n) {if(n==0 || n==1 || n==2) return n; int[] result = new int[n+1]; result[0] = 0; result[1] = 1; result[2] = 2; for(int i=3;i<n+1;i++){ result[i] = result[i-1] + result[i-2]; } return result[n]; }}
0 0
- LeetCode OJ算法题(七十):Climbing Stairs
- LeetCode OJ:Climbing Stairs
- LeetCode OJ - Climbing Stairs
- [LeetCode OJ]Climbing Stairs
- LeetCode OJ Climbing Stairs
- <LeetCode OJ> 70. Climbing Stairs
- [leetcode oj 70]Climbing Stairs
- LeetCode OJ 之 Climbing Stairs ( 爬楼梯 )
- Leetcode OJ 70 Climbing Stairs [Easy]
- LeetCode(70) Climbing Stairs
- LeetCode(70) Climbing Stairs
- 【算法作业11】LeetCode 70. Climbing Stairs
- Leetcode算法学习日志-70 Climbing Stairs
- LeetCode OJ-70. Climbing Stairs(爬楼梯问题)
- [leetcode刷题系列]Climbing Stairs
- LeetCode刷题笔录Climbing Stairs
- LeetCode第70题 Climbing Stairs
- leetcode 每日一题 70. Climbing Stairs
- qt 程序启动参数 -qws
- 使用邮件发送运行时间久的SQL语句
- malloc && calloc
- leetcode
- 什么叫编译时和运行时
- LeetCode OJ算法题(七十):Climbing Stairs
- kafka java示例
- POJ 3321—— Apple Tree(树状数组)
- webots控制器编写
- ZOJ Problem Set - 3666 Alice and Bob(SG)
- 介绍如何写windows下的批处理程序.
- Java基础视频教程-面向对象
- 查询 SQL Server 系统目录常见问题
- NYOJ-So Easy[Ⅰ](三角形外接圆半径)