题目

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. C# 常见的字符串操作

    例1: 遍历字符串中的每一个字符: string src = "aa-b - c-a - d-e- d-e- a- a-b-cc"; foreach(char c in src) ...

  2. 【ros】【bug】gtk2\3 冲突

    ORBSLAM2首次运行出现GTK冲突. Gtk-ERROR **: GTK+ 3 symbols detected. Using GTK+ 2.x and GTK+ 3 in the same pr ...

  3. 【Python音乐生成】这是一个超棒的dataset

    http://colinraffel.com/projects/lmd/

  4. 详细步骤教你安装yii高级应用程序和配置composer环境

    现在开始工作,应公司的要求,要开始接触yii了,作为一个没有碰过yii的小白,首先一个问题就是怎么去安装高级程序应用,过程不麻烦,但是也需要细心和耐心,百度资料里面的教程都不太全,漏这漏那的,所以在这 ...

  5. windows下php7.1.5、mysql环境搭建

    php http://windows.php.net/download/ 如果是使用ISAPI的方式来运行PHP就必须用Thread Safe(线程安全)的版本:而用FastCGI模式运行PHP的话就 ...

  6. linux 命令——15 tail (转)

    tail 命令从指定点开始将文件写到标准输出.使用tail命令的-f选项可以方便的查阅正在改变的日志文件,tail -f filename会把filename里最尾部的内容显示在屏幕上,并且不但刷新, ...

  7. hdu1863 畅通工程---MST&连通

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1863 题目大意: 中文题,求MST权值,如果不连通,输出? 解题思路: 这道题帮我找出了之前模板中的 ...

  8. Android(java)学习笔记94: SurfaceView使用

    1. SurfaceView简介    在一般的情况下,应用程序的View都是在相同的GUI线程(UI主线程)中绘制的.这个主应用程序线程同时也用来处理所有的用户交互(例如,按钮单击或者文本输入). ...

  9. java基础面试题:说说&和&&的区别

    &与&&都是逻辑与 不同的是&左右两边的判断都要进行,而&&是短路与,当&&左边条件为假则不用再判断右边条件,所以效率更高 例如,对于i ...

  10. 文档处理jQuery,实现添加删除复制

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...