[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 ...
随机推荐
- android可拖动排序GridView实现
经常使用今日头条.网易新闻的同学们应该都会注意到用于管理多个频道的可拖动排序GridView,下面介绍一下可拖动的DragGridView的实现方法.代码放在GitHub上https://github ...
- 开发移动端web的一些知识
由于智能机的普及,越来越多网页支持移动端了,那么如何解决适配移动端呢 在这总结一下自己的学习笔记 viewport:虚拟的容器,仅在移动设备有效 <meta name="viewpor ...
- Asp.Net写文本日志
底层代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespac ...
- 2014.9.20CSS样式表
一.前景与背景 background-color: 背景色,定义背景的颜色 background-image:url() 定义背景图片 background-attachment:fixed/scro ...
- 在Windows7防火墙允许指定的端口
在xp系统的时代,修改防火墙很方便,很简单.windows7或许是做得过于复杂了.当然所谓安全性也是相当于其他之前版本的系统更高了.为什么要打开端口,肯定是在windows7下启动了网络服务,需要开启 ...
- poj2407---欧拉函数应用
欧拉函数介绍: 在数论中,对正整数n,欧拉函数是少于或等于n你的数中与n互质的数的数目. 通式:φ(x)=x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/pn),其中 ...
- SGU 248. Integer Linear Programming( 背包dp )
看了半天...发现就是个背包...然后就不打算敲了. 看了一眼forum..顿时吓傻..其他人用了gcd啊什么的各种奇怪的东西..然后还是敲了个背包结果就AC了= =既然写了代码就扔上来吧... -- ...
- win8 iis安装及网站发布(转)
系统:win8 环境:vs2012 一:安装IIS 比较win7的安装来说,多选了几个钩钩,不然会报错,偶就遇到这样的错误. 控制面板->程序和功能->启动和关闭windows功能,钩钩图 ...
- Java学习之网络编程
转自:http://blog.csdn.net/driverking/article/details/6573992 一.网络编程基本概念 1.OSI与TCP/IP体系模型 2.IP和端口 解决了文章 ...
- [原创]Python批量操作文件,批量合并
最近几个小伙伴在手动合并一些文本文件,感觉可以用Python批量实现,就有了这段代码 import os import re import sys def printEnter(f1): #每两个文件 ...