【Climbing Stairs】cpp
题目:
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的更多相关文章
- hdu 4739【位运算】.cpp
题意: 给出n个地雷所在位置,正好能够组成正方形的地雷就可以拿走..为了简化题目,只考虑平行于横轴的正方形.. 问最多可以拿走多少个正方形.. 思路: 先找出可以组成正方形的地雷组合cnt个.. 然后 ...
- 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~ ...
- 【Valid Sudoku】cpp
题目: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could ...
- 【Permutations II】cpp
题目: Given a collection of numbers that might contain duplicates, return all possible unique permutat ...
- 【Subsets II】cpp
题目: Given a collection of integers that might contain duplicates, nums, return all possible subsets. ...
- 【Sort Colors】cpp
题目: Given an array with n objects colored red, white or blue, sort them so that objects of the same ...
- 【Sort List】cpp
题目: Sort a linked list in O(n log n) time using constant space complexity. 代码: /** * Definition for ...
- 【Path Sum】cpp
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 【Symmetric Tree】cpp
题目: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). F ...
随机推荐
- GitLab关于SSH的使用
SSH Git是分布式版本控制系统,这意味着您可以在本地工作,但您也可以将更改共享或“推送”到其他服务器.在将更改推送到GitLab服务器之前,您需要一个用于共享信息的安全通信通道. SSH协议提供此 ...
- jquery datatable 获取当前分页的数据
使用jquery datatable 遇到分页分别求和时,找了半天才找到获取当前分页数据的方法,以此总结 var table=$('#example').DataTable( { "pagi ...
- 8. String to Integer
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- 简析平衡树(一)——替罪羊树 Scapegoat Tree
前言 平衡树在我的心目中,一直都是一个很高深莫测的数据结构.不过,由于最近做的题目的题解中经常出现"平衡树"这三个字,我决定从最简单的替罪羊树开始,好好学习平衡树. 简介 替罪羊树 ...
- 5.3 Date类型
创建一个日期对象: var now = new Date( ); var now= new Date(); document.write(now); //Tue Apr 19 2016 11:43:5 ...
- javaweb基础(29)_EL表达式
一.EL表达式简介 EL 全名为Expression Language.EL主要作用: 1.获取数据 EL表达式主要用于替换JSP页面中的脚本表达式,以从各种类型的web域 中检索java对象.获取数 ...
- React 服务端渲染最佳解决方案
最近在开发一个服务端渲染工具,通过一篇小文大致介绍下服务端渲染,和服务端渲染的方式方法.在此文后面有两中服务端渲染方式的构思,根据你对服务端渲染的利弊权衡,你会选择哪一种服务端渲染方式呢? 什么是服务 ...
- Oracle数据库学习(二)
2.用SQL进行多表查询 (1)无条件多表查询 笛卡尔集:总记录数=table1记录数×table2记录数 select * from table1, table2 (2)等值连接 内连接:selec ...
- java抓取12306火车余票信息
最近在弄一个微信的公众帐号,涉及到火车票查询,之前用的网上找到的一个接口,但只能查到火车时刻表,12306又没有提供专门的查票的接口.今天突然想起自己直接去12306上查询,抓取查询返回的数据包,这样 ...
- javascript oo实现
很久很久以前,我还是个phper,第一次接触javascript觉得好神奇.跟传统的oo类概念差别很大.记得刚毕业面试,如何在javascript里面实现class一直是很热门的面试题,当前面试百度就 ...