LintCode 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?
Example
Given an example n=3 , 1+1+1=2+1=1+2=3
return 3
For the problem, try to think about it in this way.
Firstly, we define the problem of DP(n) as the ways of approaching to n stairs.
The problem of DP(n) depends on DP(n-1) and DP(n-2). Then the DP(n) = DP(n-1) + DP(n-2). Because there is two possibilities for DP(n) happen due to the rule that either it is accomplished by step 1 or step 2 stairs before approaching to n.
Initialize the DP(0) =1 and DP(1) = 1.
Solve problem DP(n)
public class Solution {
/**
* @param n: An integer
* @return: An integer
*/
public int climbStairs(int n) {
// write your code here
if (n <= 1) {
return 1;
}
int last = 1, lastlast = 1;
int now = 0;
for (int i = 1; i < n; i++) {
now = last + lastlast;
lastlast = last;
last = now;
}
return now;
}
}
Then this problem is a fibonacci sequence
LintCode Climbing Stairs的更多相关文章
- [LintCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LeetCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Leetcode: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- Climbing Stairs
Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...
- 3月3日(6) Climbing Stairs
原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
- 【LeetCode练习题】Climbing Stairs
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you c ...
- 42. leetcode 70. Climbing Stairs
70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time y ...
随机推荐
- Git 仓库和记录操作到仓库
Git 配置好了,来 clone 个或者新建个仓库来试试, $ git clone git@github.com:git/git.git 把 Git 的源码克隆下来,克隆会自动创建本地仓库,并创建本地 ...
- ios企业应用部署
最近公司要整一套企业内部用的应用,ios版本不上线要求可以随时下载使用,先是申请了企业者开发账号,然后发布应用,部署在自己服务器上供用户下载安装. 第一步:准备好应用相关的东西,基本上就是两个文件,x ...
- PHP基础教程-54课-问题
question: $arr = array(1,2,3,4); /*如何通过foreach 将数组变成 $arr = arry(2,4,6,8) */ 起初用: $arr = array(1,2,3 ...
- OC中用NSSortDescriptor对象进行数组排序
//创建一个数组 NSArray *array = @[@"one", @"two", @"three", @"four" ...
- wordpress 首页调用文章 不同样式的方法
<?php $count = 1; $display_categories = array(1); foreach ($display_categories as $category) { ?& ...
- ubuntu 使用中的一些问题汇总
1.IOError: [Errno 13] Permission denied /usr/local…… 这个错误是在terminal中运行pip install 时产生的,说的时没有权限运行安装包, ...
- useful tips for win7--close the noise volume(关掉win7开机、系统操作的声音)
how to close the voice of your PC? i) open your computer and then press F2 to enter the BIOS set-u ...
- android-自定义控件之液位指示器
由于安卓应用很广泛,在工业中也常有一些应用,比如可以用安卓来去工业中的一些数据进行实现的监测,显示,同时可以做一些自动化控制,当然在这里,我不是做这些自动化控制方面的研究,只是做一个控件,液位指示,其 ...
- 黑马程序员——C语言基础 函数
Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)函数的定义 1> 任何一个C语言程序都是由一个或者多个程序段( ...
- CFD计算
47 求解器为flunet5/6在设置边界条件时,specify boundary types下的types中有三项关于interior,interface,internal设置,在什么情况下设置相应 ...