[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 ...
随机推荐
- mac下通过xcodebuild使用oclint
step1 :下载oclint并安装 下载地址: http://oclint.org/downloads.html 选择mac os x或者darwin的包,下载到本地. 文件夹类似以下: oclin ...
- 编写可维护的JS 06
7.事件处理 //典型用法 function handlerClick(event){ var popup = document.getElementById('popup'); popup.styl ...
- JDK源码学习--String篇(四) 终结篇
StringBuilder和StringBuffer 前面讲到String是不可变的,如果需要可变的字符串将如何使用和操作呢?JAVA提供了连个操作可变字符串的类,StringBuilder和Stri ...
- 今年暑假不AC1
Description "今年暑假不AC?" "是的." "那你干什么呢?" "看世界杯呀,笨蛋!" " ...
- BZOJ 1066: [SCOI2007]蜥蜴( 最大流 )
结点容量..拆点然后随便写 --------------------------------------------------------------- #include<cstdio> ...
- 利用Console来调试JS程序、Console用法总结
http://blog.163.com/zhangmihuo_2007/blog/static/27011075201452522824347/ http://blog.163.com/zhangmi ...
- 动画画圆的效果特效ios源码
一款不错的支持动画画圆的效果特效源码,该效果实现了动画画圆,还可以扩展成画其他平面图形功能等,大家可以下载看看吧. //定义所需要画的图形 -(void)intiUIOfView { U ...
- [Android] 使用Webview进行OAUTH
1. 源起 最近在弄Google登录,Google登录要求手机上必须按照Google Play Service,有些手机比如小米,没有Google Play Servcie,因此,有必要实现一 ...
- HTML5 postMessage 和 onmessage API 具体应用
HTML5 postMessage 和 onmessage API 具体应用 随着 HTML5 的发展.了解并熟悉 HTML5 的 API 接口是很重要的.postMessage(send) 和 on ...
- Android 实现左右滑动效果ViewFlipper终结【转】
本示例演示在Android中实现图片左右滑动效果. 关于滑动效果,在Android中用得比较多,本示例实现的滑动效果是使用ViewFlipper来实现的,当然也可以使用其它的View来实现.接下来 ...