LeetCode(70) 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?
分析
这个题目是一个计算n层阶梯情况下,走到顶端的路径种数(要求每次只能上1层或者2层阶梯)。
这是一个动态规划的题目:
n = 1 时 ways = 1;
n = 2 时 ways = 2;
n = 3 时 ways = 3;
…
n = k 时 ways = ways[k-1] + ways[k-2];
明显的,这是著名的斐波那契数列问题。有递归和非递归两种方式求解,但是亲测递归方式会出现 The Time Limit异常,所以只能采用非递归计算,可以用一个动态数组保存结果。
AC代码
class Solution {
public:
int climbStairs(int n) {
//如果0层,返回0
if (n <= 0)
return 0;
//如果只有1层阶梯,则只有一种方式
else if (n == 1)
return 1;
//若有两层阶梯,则有两种方式(每次走一层,一次走两层)
else if (n == 2)
return 2;
int *r = new int[n];
//其余的情况方式总数 = 最终剩余1层的方式 + 最终剩余两层阶梯的方式
r[0] = 1;
r[1] = 2;
for (int i = 2; i < n; i++)
r[i] = r[i - 1] + r[i - 2];
int ret = r[n - 1];
delete []r;
return ret;
}
};
LeetCode(70) Climbing Stairs的更多相关文章
- LeetCode之旅(16)-Climbing Stairs
题目描述: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either ...
- LeetCode(70): 爬楼梯
Easy! 题目描述: 假设你正在爬楼梯.需要 n 步你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 ...
- Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs)
Leetcode之动态规划(DP)专题-746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 数组的每个索引做为一个阶梯,第 i个阶梯对应着一个非负数的体力花费值 cost ...
- Qt 学习之路 2(70):进程间通信
Qt 学习之路 2(70):进程间通信 豆子 2013年11月12日 Qt 学习之路 2 16条评论 上一章我们了解了有关进程的基本知识.我们将进程理解为相互独立的正在运行的程序.由于二者是相互独立的 ...
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
随机推荐
- [浏览器美化]去除 Firefox 当前选中标签页顶端的线条
Firefox 当前选中的标签页的最上方会显示有一条线,我觉得很丑,如图: 在地址栏输入 about:support ,然后点击打开目录. 在此目录中创建一个 chrome 文件夹(若没有),然后进入 ...
- virtualenv杂记
Linux复制命令: cp sourcedir destdir (如果是复制文件夹,增加参数 -a) Linux重命名命令:mv 旧的名称 新的名称 通过xshell查看虚拟机的配置:通过命令 ...
- JavaScript 编程艺术-第4章(JavaScript美术馆)代码
功 能:在同一个网页上切换显示不同的图片与文本(*亲测可用) 使用属性: a) document.getElementById(" ") ——返回一个与给定的id属性值的 ...
- spring 获取配置文件的值
Spring 获取配置文件的值 package com.hafiz.www.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; ...
- 《Windows核心编程系列》九谈谈同步设备IO与异步设备IO之同步设备IO
同步设备IO 所谓同步IO是指线程在发起IO请求后会被挂起,IO完成后继续执行. 异步IO是指:线程发起IO请求后并不会挂起而是继续执行.IO完毕后会得到设备的通知.而IO完成端口就是实现这种通知的很 ...
- hihoOffer收割练习20题目2
题目2 : SCI表示法 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 每一个正整数 N 都能表示成若干个连续正整数的和,例如10可以表示成1+2+3+4,15可以表示 ...
- 洛谷 P3332 [ZJOI2013]K大数查询 || bzoj3110
用树套树就很麻烦,用整体二分就成了裸题.... 错误: 1.尝试线段树套平衡树,码农,而且n*log^3(n)慢慢卡反正我觉得卡不过去 2.线段树pushdown写错...加法tag对于区间和的更新应 ...
- 转 Docker 组件如何协作?- 每天5分钟玩转容器技术(8)
http://www.cnblogs.com/CloudMan6/p/6774519.html 记得我们运行的第一个容器吗?现在通过它来体会一下 Docker 各个组件是如何协作的. 容器启动过程如下 ...
- 225 Implement Stack using Queues 队列实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack.pop ...
- ssm(Spring、Springmvc、Mybatis)实战之淘淘商城-第三天(非原创)
文章大纲 一.课程介绍二.简单功能实现三.图片上传功能实战四.项目源码与资料下载五.参考文章 一.课程介绍 一共14天课程(1)第一天:电商行业的背景.淘淘商城的介绍.搭建项目工程.Svn的使用. ...