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 * ...
随机推荐
- Java Genericity
四. Java Genericity 1. Genericity 泛型 泛型 <T> 1. 泛型就是参数化类型 2. 作用:安全,方便 3. 适用于对多种数据类型执行相同功能的代码,主 ...
- shiro之jdbcRealm
Shiro认证过程 创建SecurityManager--->主体提交认证--->SecurityManager认证--->Authenticsto认证--->Realm验证 ...
- php 打包下载
<?php class zipfile { var $datasec = array (); var $ctrl_dir = array (); var $eof_ctrl_dir = &quo ...
- Django MTV模型思想
一.Django的MTV分别代表: 1.Model(模型):负责业务对象与数据库的对象(ORM) 2.Template(模版):负责如何把页面展示给用户 3.View(视图):负责业务逻辑,并在适当的 ...
- 51nod 1134最长递增子序列
1134 最长递增子序列 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出长度为N的数组,找出这个数组的最长递增子序列.(递增子序列是指,子序列的元素 ...
- Liferay门户部署至Apusic Application Server域
Liferay默认情况下部署在Tomcat容器中,因项目原因需要将Liferay部署至金蝶Apusic Application Server应用服务器中,部署过程如下,特此记录. 1.liferay- ...
- 动态生成li标签,并设置点击事件
今天要解释的是如下界面 主要实现了: 1.模拟后台的json数据,动态生成li标签 2.导航栏的下划线 3.给li标签右边设置图片 4.动态生成的li标签,设置选中的li的点 ...
- String的用法——其他功能
package cn.itcast_06; /* String类的其他功能: 替换功能: String replace(char old,char new) String replace(String ...
- AJPFX总结关于Java中过滤出字母、数字和中文的正则表达式
1.Java中过滤出字母.数字和中文的正则表达式 (1)过滤出字母的正则表达式 [^(A-Za-z)] (2) 过滤出 数字 的正则表达式 [^(0-9)] (3) 过滤出 中文 的正则 ...
- kafaka
http://www.360doc.com/content/15/0429/12/9350055_466788393.shtml 一.Kafka中的核心概念 Producer: 特指消息的生产者 Co ...