leetcode刷题,总结,记录,备忘70
2015-06-26 22:30
351 查看
leetcode70Climbing 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为1的时候,1种方法,2的时候,2种,3的时候3种,4的时候5种,5的时候8种,当n大于3之后,就是我们所熟悉的斐波那契数列了,,,然后这个题目就很简单了。。。。话说这题要不是看看别人的提示,,我还真不会做。。。
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为1的时候,1种方法,2的时候,2种,3的时候3种,4的时候5种,5的时候8种,当n大于3之后,就是我们所熟悉的斐波那契数列了,,,然后这个题目就很简单了。。。。话说这题要不是看看别人的提示,,我还真不会做。。。
class Solution { public: int climbStairs(int n) { if (n == 1 || n == 0) return 1; if (n == 2) return 2; if (n == 3) return 3; int pre = 2, cur = 3, temp; for (int i = 4; i <= n; i++) { temp = cur; cur += pre; pre = temp; } return cur; } };
相关文章推荐
- 企业应用架构学习笔记
- spring 的注解能互相感知?
- C++中堆和栈的完全解析
- [Java]学习笔记,随笔【三】
- java 基础----抽象类与接口的区别,知道区别才能准备的应用
- IOS--OC--LessonNSArray 数组
- STM32--GPIO学习
- Axis Aligned Rectangles (Google 面试题, 2016网易游戏校招笔试)
- Can't load AMD 64-bit .dll on a IA 32-bit platform
- 验证角谷猜想(hd1279)
- 跨浏览器的placehold
- Tomcat与Servlet工作流程
- SQL Server中查找包含某个文本的存储过程
- 关于居中
- 简单模拟一下科目三的灯光考试
- Linux和Windows下控制台字体颜色设置
- 【POJ 2253】Frogger
- Java反射(四)利用反射调用方法
- Shuffle'm Up
- Java中4大基本加密算法解析