LeetCode之“动态规划”: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?
做这道题容易陷入的思维就是:所有step加起来的总和是n。俺这种思维去解决这个问题的话会显得很复杂。
按动态规划的思维,要爬到第n阶,可以从第n-1阶爬1阶到达,也可以从第n-2阶爬2阶到达,因此爬到第n阶的方法有这么多种:
dp[n] = dp[n - ] + dp[n - ]
按此思维的程序代码如下:
class Solution {
public:
int climbStairs(int n) {
if(n == || n == || n == )
return n;
int * dp = new int[n + ];
dp[] = ;
dp[] = ;
dp[] = ;
for(int i = ; i < n + ; i++)
dp[i] = dp[i - ] + dp[i - ];
return dp[n];
}
};
LeetCode之“动态规划”:Climbing Stairs的更多相关文章
- [LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- Leetcode之70. Climbing Stairs Easy
Leetcode 70 Climbing Stairs Easy https://leetcode.com/problems/climbing-stairs/ You are climbing a s ...
- 【LeetCode练习题】Climbing Stairs
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you c ...
- 【一天一道LeetCode】#70. Climbing Stairs
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- 【LeetCode】70. Climbing Stairs 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 记忆化搜索 动态规划 空间压缩DP 日期 [L ...
- Leetcode 题目整理 climbing stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 【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 ...
- LeetCode算法题-Climbing Stairs(Java实现)
这是悦乐书的第159次更新,第161篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第18题(顺位题号是70).你正在爬楼梯,它需要n步才能达到顶峰.每次你可以爬1或2步, ...
- LeetCode题解之Climbing Stairs
1.题目描述 2.问题分析 使用动态规划. 3.代码 int climbStairs(int n) { ){ return n; } ]; dp[] = ; dp[] = ; dp[] = ; ; i ...
- LeetCode OJ:Climbing Stairs(攀爬台阶)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
随机推荐
- Apache shiro集群实现 (三)shiro身份认证(Shiro Authentication)
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
- 安卓Button-TextView-EditText综合运用
1.如何使用安卓中的按键Button? 1.先从控件库拖一个按钮button的控件,在XML设置好宽高等参数 对应的就是Button这个图标,直接拖出来即可; 以下是设置这个按钮对应的XML代码: & ...
- [openresty]安装nginx_lua
这种方式是直接安装openresty ,不是通过重新编译nginx Ubuntu 安装 安装依赖包 $ sudo apt-get install libreadline-dev libncurses5 ...
- Java进阶(四十五)java 字节流与字符流的区别
java 字节流与字符流的区别(转载) 字节流与和字符流的使用非常相似,两者除了操作代码上的不同之外,是否还有其他的不同呢? 实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作 ...
- Servlet之cookie处理
Cookies 通常设置在 HTTP 头信息中(虽然JavaScript 也可以直接在浏览器上设置一个 Cookie).设置 Cookie 的 Servlet 会发送如下的头信息: HTTP/1.1 ...
- 编译GDAL支持ArcObjects
编译GDAL支持ArcObjects. 首先修改nmake.opt文件中对应的ArcObjects,修改后的如下所示: #uncomment to use ArcObjects ARCOBJECTS_ ...
- Android的ToggleButton和Switch以及AnalogColok和DigitalColok的用法-android学习之旅(二十)
ToggleButton 和Switch简介 ToggleButton 和Switch都是继承了Button,所以他们的属性设置和Button差不多. 分别支持的属性 ToggleButton 的属性 ...
- JAVA代码规范笔记(上)
本文为<code conventions-150003>(JAVA代码规范)笔记. 文件组织 1.超过2000行代码的源文件将会比较难以阅读,应该避免. 2.每个Java源文件都包含单一的 ...
- C++中const加强
demo // C语言中的const是一个冒牌货 int main() { // 好像a是一个常量 const int a = 10; int *p = NULL; p = (int *)&a ...
- Linux内核基础
Linux系统运行的应用程序通过系统调用来与内核通信.应用程序通常调用库函数(比如C库函数)再有库函数通过系统调用界面,让内核带其完成各种不同的任务. 下面这张图显示的就是应用程序,内 ...