【leetcode❤python】70. Climbing Stairs
#Method1:动态规划
##当有n个台阶时,可供选择的走法可以分两类:
###1,先跨一阶再跨完剩下n-1阶;
###2,先跨2阶再跨完剩下n-2阶。
###所以n阶的不同走法的数目是n-1阶和n-2阶的走法数的和
class Solution(object):
def climbStairs(self, n):
if n==1 or n==2 or n==0:
return n
steps=[1,1]
for i in xrange(2,n+1):
steps.append(steps[i-1]+steps[i-2])
return steps[n]
#Method2:找规律
###当输入为1, 2, 3, 4, 5, 6, 7, 8, 9, 10时,
###观察输出为1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
###斐波那契数列
class Solution(object):
def climbStairs(self,n):
pre=cur=1
for i in xrange(1,n):
pre,cur=cur,cur+pre
return cur
【leetcode❤python】70. Climbing Stairs的更多相关文章
- [LeetCode&Python] Problem 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练题——70. Climbing Stairs
1.题目 70. Climbing Stairs——Easy You are climbing a stair case. It takes n steps to reach to the top. ...
- 【leetcode❤python】Sum Of Two Number
#-*- coding: UTF-8 -*- #既然不能使用加法和减法,那么就用位操作.下面以计算5+4的例子说明如何用位操作实现加法:#1. 用二进制表示两个加数,a=5=0101,b=4=0100 ...
- 【LeetCode】70. Climbing Stairs 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目大意 题目大意 解题方法 递归 记忆化搜索 动态规划 空间压缩DP 日期 [L ...
- 【一天一道LeetCode】#70. Climbing Stairs
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 You are ...
- 【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❤python】 1. Two Sum
#-*- coding: UTF-8 -*- #AC源码[意外惊喜,还以为会超时]class Solution(object): def twoSum(self, nums, target): ...
- 【leetcode❤python】 58. Length of Last Word
#-*- coding: UTF-8 -*-#利用strip函数去掉字符串去除空格(其实是去除两边[左边和右边]空格)#利用split分离字符串成列表class Solution(object): ...
- 【leetcode❤python】 8. String to Integer (atoi)
#-*- coding: UTF-8 -*-#需要考虑多种情况#以下几种是可以返回的数值#1.以0开头的字符串,如01201215#2.以正负号开头的字符串,如'+121215':'-1215489' ...
随机推荐
- 类名.class, class.forName(), getClass()区别
1:Class cl=A.class; JVM将使用类A的类装载器, 将类A装入内存(前提是:类A还没有装入内存),不对类A做类的初始化工作.返回类A的Class的对象. 2:Class cl=对象引 ...
- 解决filezilla中无法显示中文的文件名
设定字符集时选择自定义字符集, 然后输入字符集为 GBK
- zw版【转发·台湾nvp系列Delphi例程】HALCON SetLineStyle1
zw版[转发·台湾nvp系列Delphi例程]HALCON SetLineStyle1 procedure TForm1.Button1Click(Sender: TObject);var img : ...
- zw版【转发·台湾nvp系列Delphi例程】HALCON SetLineStyle2
zw版[转发·台湾nvp系列Delphi例程]HALCON SetLineStyle2 procedure TForm1.Button1Click(Sender: TObject);var img : ...
- yii Html中的a标签使用
1.use yii\helpers\Html; <?php echo Html::a('编辑',['edit','id'=>$info['goods_id']])?> 2.有确认框的 ...
- CSS的样式合并与模块化
by zhangxinxu from http://www.zhangxinxu.com 原文地址:http://www.zhangxinxu.com/wordpress/?p=931 一.引言 本文 ...
- 对比其它软件方法评估敏捷和Scrum
一般来说,选择一种软件开发方法,更像是加入一个邪教组织,而不像是做出了一个技术决策.许多公司甚至从未试图去评估这些方法,而仅仅是盲目采用最流行的方法,这就造成了如今五花八门的各种敏捷方法.因此本文将使 ...
- 输入框焦点时自动清除value
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <script typ ...
- Java JDBC 驱动 MySQL
MySQL: 1>下载地址:http://www.mysql.com/products/connector/ 2> //jdbc:[数据库类型]://[ip地址]:[端口号]/[数据库名] ...
- ACM第一站————快速排序
转载请注明出处,谢谢!http://www.cnblogs.com/Asimple/p/5455125.html 快速排序(Quicksort)是对冒泡排序的一种改进. 快速排序由C. A. ...