剑指Offer面试题9 & Leetcode70
来源:互联网 发布:淘宝代销下单好吗 编辑:程序博客网 时间:2024/05/23 11:33
剑指Offer面试题9 & Leetcode70
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?
解题思路
考虑:爬楼梯的方法数相当于斐波那契数列求序号n处数列值。可以使用动态规划,写出递推关系式f(n)=f(n-1)+f(n-2),用两个int值保存f(n-1)和f(n-2),注意递推的初始值,即一阶台阶的方法数为1,二阶台阶的方法数为2,循环求出值。
Solution
public int climbStairs(int n) { if(n==1) return 1; else if(n==2) return 2; else{ int num_n_1 = 2; int num_n_2 = 1; for(int i=3;i<=n;i++){ int temp = num_n_1 + num_n_2; num_n_2 = num_n_1; num_n_1 = temp; } return num_n_1; } }
1 0
- 剑指Offer面试题9 & Leetcode70
- 剑指offer面试题9
- 剑指offer 面试题9
- 剑指offer 面试题9
- 【面试题】剑指offer 9
- 剑指offer 面试题
- 剑指offer面试题
- 剑指offer面试题总结
- 剑指offer面试题06
- 剑指offer面试题 04
- 剑指offer 面试题07
- 剑指offer面试题08
- 剑指offer面试题42
- 剑指offer面试题1
- 剑指offer面试题3
- 剑指offer面试题4
- 剑指offer面试题5
- 剑指offer面试题6
- Redis Sentinel实现的机制与原理详解
- Linux RPM包管理-rpm命令
- ionic加载动画
- python3.6.0(Anaconda)安装PyQt5,“DLL load failed: 找不到指定的模块。”
- 选购和配置阿里云服务器(Java web版)
- 剑指Offer面试题9 & Leetcode70
- 为什么单例模式中要使用静态变量
- NodeJS 异步操作:摆脱‘Callback hell’初谈
- 用java做一个2048小游戏
- vs shfit+ctrl+f 全局搜索失
- 把GIT导出的项目转换成Maven项目
- mybatis系列三:springMVC和mybatis的运用
- 基于 Python 的数据结构与算法分析学习记录(6-9)—— 二叉堆操作
- Java信号量Semaphor解析