题目

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?

代码

class Solution {
public:
int climbStairs(int n) {
int prev = ;
int curr = ;
for (int i = ; i<n; ++i)
{
const int tmp = curr;
curr = curr + prev;
prev = tmp;
}
return curr;
}
};

Tips:

1. 这道题可以抽象成求Fibonacci数列的通项(只看最后一步:从n-1到n迈一个台阶;从n-2到n迈两个台阶)

==================================================

第二次过这道题,没有想到直接用斐波那契数列的思想。

首先想到的做法是直接递归,结果超时;后来想到可以用一个记忆hashmap,保存计算过的中间结果,于是有了下面0msAC的代码。

class Solution {
public:
int climbStairs(int n)
{
map<int, int> calculated;
calculated[] = ;
calculated[] = ;
return Solution::subStairs(n, calculated);
}
static int subStairs(int n, map<int, int>& calculated)
{
if ( n== ) return ;
if ( n== ) return ;
int way1 = calculated.find(n-)!=calculated.end() ? calculated[n-] : calculated[n-]=Solution::subStairs(n-, calculated);
int way2 = calculated.find(n-)!=calculated.end() ? calculated[n-] : calculated[n-]=Solution::subStairs(n-, calculated);
return way1+way2;
}
};

tips:

这个思路还是非常通用的。

原来斐波那契数列思路的非递归实现也要记住,这段代码也很漂亮。

【Climbing Stairs】cpp的更多相关文章

  1. hdu 4739【位运算】.cpp

    题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...

  2. Hdu 4734 【数位DP】.cpp

    题意: 我们定义十进制数x的权值为f(x) = a(n)*2^(n-1)+a(n-1)*2(n-2)+...a(2)*2+a(1)*1,a(i)表示十进制数x中第i位的数字. 题目给出a,b,求出0~ ...

  3. 【Valid Sudoku】cpp

    题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...

  4. 【Permutations II】cpp

    题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...

  5. 【Subsets II】cpp

    题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...

  6. 【Sort Colors】cpp

    题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...

  7. 【Sort List】cpp

    题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...

  8. 【Path Sum】cpp

    题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...

  9. 【Symmetric Tree】cpp

    题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...

随机推荐

  1. Callable的简单使用

    说起java的线程操作,都会想到Thread和Runable这两个, 这两个类可以实现异步和同步. 在大多数的java开发中, 这两个都是实现异步的线程来使用, 但是现在考虑一种情况: 发出一条线程, ...

  2. zabbix文档3.4-7配置

    zabbix文档3.4-7配置 1 主机和主机组 典型的Zabbix主机是您希望监视的设备(服务器,工作站,交换机等). 创建主机是Zabbix中首个监控任务之一.例如,如果要监视服务器"x ...

  3. UVA 10375 Choose and divide(大数的表示)

    紫上给得比较奇怪,其实没有必要用唯一分解定理.我觉得这道题用唯一分解只是为了表示大数. 但是分解得到的幂,累乘的时候如果顺序很奇怪也可能溢出.其实直接边乘边除就好了.因为答案保证不会溢出, 设定一个精 ...

  4. POJ 2718 Smallest Difference(dfs,剪枝)

    枚举两个排列以及有那些数字,用dfs比较灵活. dfs1是枚举长度短小的那个数字,dfs2会枚举到比较大的数字,然后我们希望低位数字的差尽量大, 后面最优全是0,如果全是0都没有当前ans小的话就剪掉 ...

  5. 【洛谷2216】[HAOI2007] 理想的正方形(二维RMQ)

    点此看题面 大致题意: 求出一个矩阵中所有\(n*n\)正方形中极差的最小值. 另一种做法 听说这题可以用单调队列去做,但是我写了一个二维\(RMQ\). 二维\(RMQ\) \(RMQ\)相信大家都 ...

  6. 从用户访问网站流程开始,细说web网络基础

    1.用户访问网站流程框架 2.dns解析原理 3.tcp/ip三次握手过程原理,11种连接状态 4.tcp/ip四次挥手过程原理,11种连接状态 5.http协议原理(www服务的请求过程)请求细节, ...

  7. 将bat批处理文件注册成windows服务

    C:\Users\lenovo>sc create MyService binPath= "C:\Program Files\restartOracle.bat"  type ...

  8. theano 模块 MLP示例

    theano 模块 MLP示例,有需要的朋友可以参考下. theano教程Example: MLP: 约定数组为列向量, 层级:将多层传感器定义为一连串的层级,每个层级定义为一个类.类属性包括:权重. ...

  9. 浏览器 DNS缓存与DNS prefetch (DNS预解析)

    浏览器 DNS缓存 浏览器DNS缓存的时间跟DNS服务器返回的TTL值无关. 注:TTL(Time-To-Live),就是一条域名解析记录在DNS服务器中的存留时间. 浏览器在获取网站域名的实际IP地 ...

  10. CSS3和动画

    定位: z-index叠层    数字越大越往上层 注意:要用z-index属性必须设position属性 溢出:overflow 属性值:visible    不剪切内容也不添加滚动条 Auto   ...