[LeetCode]题解(python):070-Climbing Stairs
题目来源:
https://leetcode.com/problems/climbing-stairs/
题意分析:
爬楼梯,一次可以爬一步或者两步。如果要爬n层,问一共有多少种爬法。比如说,如果是3层,可以有[[1,1,1],[1,2],[2,1]]共3种方法。
题目思路:
这是一个典型的动态规划问题。n层的方法数 = n - 1层的方法数 + n - 2层的方法数。
代码(Python):
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
"""
if n == 1 or n == 0:
return 1
i,tmp1,tmp2 = 2,1,1
while i <= n:
tmp1 = tmp1 + tmp2
if i == n:
return tmp1
i += 1
tmp2 = tmp1 + tmp2
if i == n:
return tmp2
i += 1
转载请注明出处:http://www.cnblogs.com/chruny/p/5045540.html
[LeetCode]题解(python):070-Climbing Stairs的更多相关文章
- leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution)
leetcode 746. Min Cost Climbing Stairs(easy understanding dp solution) On a staircase, the i-th step ...
- [LeetCode] 746. 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)
70. 爬楼梯 70. Climbing Stairs 题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意: 给定 ...
- LN : leetcode 746 Min Cost Climbing Stairs
lc 746 Min Cost Climbing Stairs 746 Min Cost Climbing Stairs On a staircase, the i-th step has some ...
- LeetCode练题——70. Climbing Stairs
1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...
- 【LeetCode】070. Climbing Stairs
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- Java for LeetCode 070 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
题目链接 题目要求 You are climbing a stair case. It takes n steps to reach to the top. Each time you can eit ...
- 070 Climbing Stairs
你正在爬楼梯.需要 n 步你才能到达顶部.每次你可以爬 1 或 2 个台阶.你有多少种不同的方式可以爬到楼顶呢?注意:给定 n 将是一个正整数.示例 1:输入: 2输出: 2说明: 有两种方法可以爬到 ...
- LeetCode(70) Climbing Stairs
题目 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cli ...
随机推荐
- ruby简单的基础 5
方法和代码块 在Ruby中.{}或do...end之间的代码是一个代码块.代码块只能出如今一个方法的后边,它紧接在方法最后一个參数的同一行上,一般由yieldkeyword调用代码块中的代码. 方法是 ...
- Android getReadableDatabase() 和 getWritableDatabase()
Android使用getWritableDatabase()和getReadableDatabase()方法都可以获取一个用于操作数据库的SQLiteDatabase实例.(getReadableDa ...
- C#高级编程三十天----泛型结构,泛型方法,泛型托付
泛型结构 泛型结构和泛型类差点儿是一直的,仅仅是泛型结构没有继承的特性..NET平台提供的一个泛型结构是(可空类型)Nullablle<T>.可空类型的引入,主要是为了解决数据库语言中的数 ...
- 关于虚拟机装kali-linux的联网问题
我用的是VMware Workstation11,近期装了一个kali-linux,想玩一下password破解.没想到装上之后网络连接显示的是活跃的却无法上网,我试过桥接等其他方式去联网,却依然无法 ...
- 转载 C# 序列化与反序列化意义详解
C# 序列化与反序列化意义详解 总结: ①序列化基本是指把一个对象保存到文件或流中,比如可以把文件序列化以保存到Xml中,或一个磁盘文件中②序列化以某种存储形式使自定义对象持久化: ③将对象从一个地方 ...
- sql中将null转换为空
sql中varchar的默认值为null 当在页面绑定数据时就会出现无法绑定情况此时就需要在查询时转换为空,, isnull(key,'') key 为字段名,后面的参数就是空值
- html5 meta标签
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-sca ...
- 解决jni链接时找不到函数的问题
用jni调用库函数时,经常会碰到link的错误,具体出错信息如下: 08-07 01:42:06.490: E/AndroidRuntime(1665): java.lang.UnsatisfiedL ...
- nodejs:注册登录session出错以及连接Mongodb数据库时Error connecting to database解决方案
(1)nodejs:注册登录session出错 解决办法: 在app.js 中将var MongoStore = require(connect-mongo')改为var MongoStore = ...
- 常用Android快速开发框架
在做项目的过程中遇到了很多困难,于是收集了一些快速开发的框架,使用后大大提高了项目开发速度,无论什么项目都可以使用的到,在此分享给大家,希望能对大家有帮助!(个人建议:有时间的同学可以看一下这些优秀框 ...