LeetCode——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?
原题链接:https://oj.leetcode.com/problems/climbing-stairs/
题目:你在爬楼梯。
须要 n 步才干到顶部。
每次你爬1 或 2 步。
有多少种独立的爬到顶部的方式?
思路:首先非常easy就想到了递归的解法。可是超时了。
public int climbStairs(int n) {
if(n < 0)
return 0;
if(n <= 1)
return 1;
return climbStairs(n - 1) + climbStairs(n - 2);
}
所以採用非递归的方式。事实上此题类似于求斐波那契数列的和,可是递归不仅慢还可能溢出。以下採用非递归的方法,当中pre代表前n-1台阶的方法数,current代表第n台阶的方法数。
public int climbStairs(int n) {
if (n == 0 || n == 1)
return 1;
int pre = 1;
int current = 1;
for (int i = 2; i <= n; i++) {
int temp = current + pre;
pre = current;
current = temp;
}
return current;
}
LeetCode——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 ...
- Leetcode: climbing stairs
July 28, 2015 Problem statement: You are climbing a stair case. It takes n steps to reach to the top ...
- [LeetCode] Climbing Stairs (Sequence DP)
Climbing Stairs https://oj.leetcode.com/problems/climbing-stairs/ You are climbing a stair case. It ...
- LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)
题目链接 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either c ...
- [Leetcode] 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 either climb ...
- leetcode Climbing Stairs python
class Solution(object): def climbStairs(self, n): """ :type n: int :rtype: int " ...
- [LeetCode] Min Cost Climbing Stairs 爬楼梯的最小损失
On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once you pay ...
- Min Cost Climbing Stairs - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Min Cost Climbing Stairs - LeetCode 注意点 注意边界条件 解法 解法一:这道题也是一道dp题.dp[i]表示爬到第i层 ...
随机推荐
- U6Linux的文件权限与目录配置
1.ll查看文件信息:[权限][连接][所有者][用户组][文件容量][修改日期][文件名] 2.第一个字符代表文件的属性:若为[d]则是目录.若为[-]则是文件.若为[l]则为连接. 3.chgrp ...
- wamp在win7下64位系统memcache/memcached安装教程
折腾了1个多小时,终于搞定.操作系统时64位的,php5.3.13 类似于上一篇的xdebug安装教程~~ memcache和memcached的区别 在自己的新程序中打算全面应用memcached ...
- 【NOIP2002】矩形覆盖 DFS
首先,我喜欢愤怒搜索,因为尽管说K<=4,的确K小于或等于3的. 当然某些K<=3还600ms的我就不加评论了. 好吧,可是怎么搜呢?我们考虑到矩形数量非常少,所以能够怒搜矩形. 一些神做 ...
- Mysql insert声明优化
1) 假设你同一时候从同一客户插入非常多行,使用多个值表的INSERT语句. 这比使用分开INSERT语句快(在一些情况中几倍). Insert into test values(1,2),(1 ...
- git 仓库
从 Git 删除文件 rm test.txt git rm test.txt 加入远程仓库 $ git remote origin $ git remote add pb git://github.c ...
- 源码安装apache及配置转发
一. 安装Apache a) 解压:tar -xvf httpd-*; b) ./configure --prefix=/usr/oracle/apache CC="gcc ...
- EXCEL随机密码生成函数
=CHAR(INT(RAND()*+))&INT(RAND()*+)&CHAR(INT(RAND()*+))&INT(RAND()*+)&CHAR(INT(RAND() ...
- 十步完全理解SQL(转)
本文由 伯乐在线 - 水果泡腾片 翻译.未经许可,禁止转载!英文出处:Lukas Eder.欢迎加入翻译组. 很多程序员视 SQL 为洪水猛兽.SQL 是一种为数不多的声明性语言,它的运行方式完全不同 ...
- Google Protocol Buffers和java字符串处理控制
大多数的操作码被从夜晚复制.懒得敲. 直接在源代码和测试结果如下. serabuffer.proto档.使用下面的命令来生成java代码. protoc -I=./ --java_out=./ ser ...
- richedit设置滚动条的位置和更新内容
需要txt发现读者richedit的scrollbar位置(为了便于下一次读,直接访问与上次读取下一个读取位置)不值得治疗,采用GetScrollPos.SetScrollPos你可以设置scorll ...