Leetcode_num13_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?
非常easy的思路。由于一次能够走1~2步,所以到达第n级能够从第n-1级,也能够从第n-2级。
设到达第n级的方法有s(n)种,s(n)=s(n-1)+s(n-2)
一開始准备用递归做。代码例如以下:
class Solution:
# @param n, an integer
# @return an integer
def climbStairs(self, n):
if n<=2:
return n
else:
return self.climbStairs(n-1)+self.climbStairs(n-2)
结果在n=35的时候TLE了。这进一步说明递归的算法效率比較低,但从思路上比較简单明了。
于是,转向迭代了,代码例如以下:
class Solution:
# @param n, an integer
# @return an integer
def climbStairs(self, n):
if n<=1:
return n
else:
s=[0 for i in range(n)]
s[0]=1 #到达第1级
s[1]=2 #到达第2级
for i in range(2,n):
s[i]=s[i-1]+s[i-2]
return s[n-1] #到达第n级
在此引入一个数组s,记录到达第n级的方法,然实际要求的返回值是s[n],数组s中的前n-1项存储值是多余的。
于是进行改进。设s1为走一步到达方法数。s2为走两步到达的方法数。那么到达第n级台阶时,s(n)=s1+s2,当中s1=s(n-1),s2=s(n-2);到达第n+1级台阶时。s(n+1)=s1+s2,当中s1=s(n)=上一步的s1+s2, s2=s(n-1)=上一步的s1,所以仅仅须要记录s1和s2的值。无需记录n个值
class Solution:
# @param n, an integer
# @return an integer
def climbStairs(self, n):
if n<=1:
return n
else:
s1=1
s2=1
for i in range(1,n):
s=s1+s2
s2=s1
s1=s
return s
这应该是比較简单的方法了。受教了。
版权声明:本文博客原创文章。博客,未经同意,不得转载。
Leetcode_num13_Climbing Stairs的更多相关文章
- [LeetCode] Climbing Stairs 爬梯子问题
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- [LintCode] 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
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- LintCode Climbing Stairs
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- 54. Search a 2D Matrix && Climbing Stairs (Easy)
Search a 2D Matrix Write an efficient algorithm that searches for a value in an m x n matrix. This m ...
- Climbing Stairs
Climbing Stairs https://leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It tak ...
- 3月3日(6) Climbing Stairs
原题 Climbing Stairs 求斐波那契数列的第N项,开始想用通项公式求解,其实一个O(n)就搞定了. class Solution { public: int climbStairs(int ...
- Cllimbing Stairs [LeetCode 70]
1- 问题描述 You are climbing a stair case. It takes n steps to reach to the top. Each time you can eithe ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
随机推荐
- MongoDB使用手册
一.安装包下载地址 http://www.mongodb.org/downloads 二.服务器端使用 LINUX版本: ①用tar -zxvf mongodb-linux-x86_64-1.2.4. ...
- Research notes for Scientific Writing
Noun. complacency n. 自得;自鸣得意 nomenclature n. 专业术语:e.g. the nomenclature of music; 音乐的专门术语 Verb. abs ...
- mysql数据导出权限问题
mysql数据导出的方法有非常多,比如mysqldump, mysql -e 'sql' > file, 这些都能够非常方便的导出数据,但是在使用普通用户导出数据的时候,出现了问题. 1 sel ...
- HDU 1251 统计难题 (字符串-Trie树)
统计难题 Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单 ...
- Mac 下安装配置Mysql
在Mac 下载 Mysql Server : 参考:http://www.mysql.com/downloads/ 下载Mysql 安装程序 打开下载地址: http://www.mysql.com/ ...
- BaseActivity--上门啦
package com.fwpt.activity; import com.fwpt.entity.RyUserInfo; import com.fwpt.entity.SmlaUserinfo; i ...
- python3语法小记(二)列表 和 元组
列表(list): 列表就像一个线性容器,但是比C++的 lis t扩展多得多 列表里的元素可以是相同类型,也可以包含各种类型,比如列表里嵌套另一个列表 >>> L1 = [1,2, ...
- 使用apache daemon让java程序在unix系统上以服务方式运行
通过使用apache_commons_daemon,可以让Java程序在unix系统上以服务器的方式运行. 当然,通过wrapper也是可以达到这样的目的,wrapper还可以指定java应用中用到的 ...
- POJ 3321 Apple Tree DFS序+fenwick
题目大意:有一颗长满苹果的苹果树,有两个操作. 1.询问以一个点为根的子树中有多少个苹果. 2.看看一个点有没有苹果,假设没有苹果.那么那里就立即长出一个苹果(= =!):否则就把那个苹果摘下来. 思 ...
- 华为OJ培训主题 比赛统计
题目例如以下: 比赛情况统计 有一个游戏平台,各个參赛队伍(以唯一的TeamID来标识)之间进行单循环的对抗赛,两个队伍之间仅仅举行一场比赛,比赛以得分的多少定胜负.须要完毕一个统计赛况的程序,可以随 ...