LeetCode Climb Stairs
来源:互联网 发布:u盘raw格式数据恢复 编辑:程序博客网 时间:2024/05/01 14:04
题目:
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?
题意:就是上楼梯的方式的种数。每次只能用1步或者是2步上楼,那么给定n阶的楼梯,共有多少种上楼梯的方式。
考虑用递归的方式,也就是每次只考虑前n-2步的时候,然后再加上后面的两阶楼梯的上法,有用1步上的或者是用2步上的。
上1阶的种类共有1种,上2阶的种类共有2种,上3阶的种类共有1+2种,上4阶的种类共有2+1+2种。。。以此类推。
public class Solution { public static int climbStairs(int n){//考虑用递归,就是有n步的台阶,那么它的种数由前n-2步的种树和最后两部的走法所决定int[] length = new int[n + 1];length[1] = 1;length[2] = 2;length[3] = 3;if(n >= 4){for(int i = 4; i <= n; i++){length[i] = length[i - 2] + length[1] + length[2];}}return length[n];}}
0 0
- LeetCode Climb Stairs
- Climb Stairs
- climb stairs
- Algorithm: Climb Stairs
- FZU--2156 Climb Stairs
- FZU 2156 Climb Stairs
- FZU 2156 Climb Stairs
- FZU 2156 Climb Stairs dp
- FZU 2165 Climb Stairs(DP)
- FZU Problem 2156 Climb Stairs DP
- leetcode - climb steps
- LeetCode: Climbing Stairs
- LeetCode: Climbing Stairs
- [LeetCode]Climbing Stairs
- LeetCode Climbing Stairs
- [Leetcode] Climbing Stairs
- Leetcode: Climbing stairs
- LeetCode Climbing Stairs
- 使用cocoapods下载很慢怎么办?
- 九月总结
- 实验一.3
- 剑指offer—用两个栈实现队列
- 9月英语小结
- LeetCode Climb Stairs
- iOS模拟器键盘的下面添加一个完成按钮
- ZooKeeper
- 生活感悟
- 【软考3】知识点的宏观总结
- 传感器的使用:方向传感器
- Git使用教程2:版本库的创建和添加文件
- 剑指offer—旋转数组的最小数字
- 编程之美变形记——“49”的个数