斐波那契数列之动态规划
2015-01-14 09:02
274 查看
有这样一道OJ
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?
https://oj.leetcode.com/problems/climbing-stairs/
咋一看不就是简单的斐波那契数列吗,递归就行了。
但是递归的话超时了。
于是用了动态规划
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?
https://oj.leetcode.com/problems/climbing-stairs/
咋一看不就是简单的斐波那契数列吗,递归就行了。
但是递归的话超时了。
于是用了动态规划
public class Solution { public int climbStairs(int n) { if(n==1) return 1; if(n==2) return 2; int pre = 1; int last = 2; for(int i=3; i<n; i++){ int tmp = last; last += pre; pre = tmp; } return last + pre; } }
相关文章推荐
- 动态规划求一个数列的最长不下降子序列java版
- 斐波那契系列问题的递归和动态规划3
- 斐波那契数的动态规划解法
- 动态规划——5 输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数, 使其和等于 m
- 动态规划-波动数列
- Java求解,输入两个整数n和m,从数列1,2,3,……n中随意取几个数,使其和等于m。要求将所有的可能组合列出来(背包动态规划问题求解)
- 斐波那契系列问题的递归和动态规划
- 动态规划--求满足条件的数列
- 动态规划 洛谷P2401 不等数列
- 类斐波那契问题中动态规划的时间复杂度优化
- 动态规划(DP计数):HDU 5116 Everlasting L
- 简单的动态规划题
- 读书笔记之编程之美 - 2.9 斐波那契(Fibonacci)数列
- 动态规划
- 动态规划专题:POJ 动态规划题目列表
- 动态规划
- 【算法学习笔记】81.动态规划 分类讨论 SJTU OJ 1075 括号匹配升级
- 动态规划之矩阵链乘
- UVA 10529 浅谈期望动态规划末状态转移推导全方程转移
- 动态规划--三角形最佳路径