题目

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. Eucalyptus镜像管理

    1.前言 Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...

  2. spring boot 下 spring security 自定义登录配置与form-login属性详解

    package zhet.sprintBoot; import org.springframework.beans.factory.annotation.Autowired;import org.sp ...

  3. byte[] 中需要除去的特定 byte

    /// <summary> /// 去掉byte[]中特定的byte /// </summary> /// <param name="SourceByteArr ...

  4. Redis基础数据结构

    Redis数据库中每个键值对都是由对象( c 的结构体对象)组成的. 数据库键总是一个字符串对象(string object) 数据库键的值可以使字符串对象.列表对象(list object).哈希对 ...

  5. Android(java)学习笔记76:Handler用法总结 和 秒表案例

    一.Handler的定义: Handler主要接收子线程发送的数据, 并用此数据配合主线程更新UI,用来跟UI主线程交互用.比如可以用handler发送一个message,然后在handler的线程中 ...

  6. 设置和重置ssh key

    查看本地是否有已经生成好的ssh key $ cat ~/.ssh/id_rsa.pub 若有,先删除: $ cd ~ $ rm -rf .ssh 重新生成ssh key ssh-keygen -t ...

  7. 一次线上CPU高的问题排查实践

    一次线上CPU高的问题排查实践 前言 近期某一天上班一开电脑,就收到了运维警报,有两台服务CPU负载很高,同时收到一线同事反馈 系统访问速度非常慢,几乎无响应. 一个美好的早晨,最怕什么就来什么.只好 ...

  8. GMap.Net解决方案之在WinForm和WPF中使用GMap.Net地图插件的开发

    在做地理位置相关的开发时,总是面临高额地图引擎费用让大部分用户望而却步,加之地图数据又是天价,那么GMap.NET就是首选了,它本身就是开源免费,服务器可以在本地缓存,以后访问时就可以直接访问. 可以 ...

  9. 问题003:JDK文件夹下的bin有什么作用?javac.exe和java.exe双击后为什么一闪而过,没了?

    bin (binary)二进制 ,JDK当中所有的可以执行的二进制应用程序都放在其中.其中都是*.exe文件,表示可以直接执行程序. javac.exe和java.exe双击后为什么一闪而过,没了?因 ...

  10. c++ 中十进制 八进制 十六进制 二进制转换 最简方法

    #include<iostream> using namespace std; int main() { int i; cin>>dec>>i; //cin> ...