070 Climbing Stairs
你正在爬楼梯。需要 n 步你才能到达顶部。
每次你可以爬 1 或 2 个台阶。你有多少种不同的方式可以爬到楼顶呢?
注意:给定 n 将是一个正整数。
示例 1:
输入: 2
输出: 2
说明: 有两种方法可以爬到顶端。
1. 1 步 + 1 步
2. 2 步
示例 2:
输入: 3
输出: 3
说明: 有三种方法可以爬到顶端。
1. 1 步 + 1 步 + 1 步
2. 1 步 + 2 步
3. 2 步 + 1 步
详见:https://leetcode.com/problems/climbing-stairs/description/
Java实现:
方法一:
class Solution {
public int climbStairs(int n) {
if(n==0||n==1||n==2){
return n;
}
return climbStairs(n-1)+climbStairs(n-2);
}
}
方法二:
class Solution {
public int climbStairs(int n) {
if(n<2){
return n;
}
int tmp=0,pre=1,res=1;
for(int i=2;i<=n;++i){
tmp=res;
res=pre+res;
pre=tmp;
}
return res;
}
}
方法三:
class Solution {
public int climbStairs(int n) {
if(n==0||n==1||n==2){
return n;
}
int[] dp=new int[n];
dp[0]=1;
dp[1]=2;
for(int i=2;i<n;++i){
dp[i]=dp[i-1]+dp[i-2];
}
return dp[n-1];
}
}
070 Climbing Stairs的更多相关文章
- 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】070. Climbing Stairs
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...
- [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 ...
- 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 ...
- leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法
Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time you ...
随机推荐
- php设置文件编码
<?php @header('Content-type: text/html;charset=UTF-8'); ?>
- 2015 年最热门的国人开发开源软件 TOP 50
开源中国在 2015 年得到了快速的发展,单开源软件收藏量就接近 40000 款,其中不乏优质的国产开源项目.本文从软件的收藏.下载.访问等多角度挑选出了 2015 年最热门的国产开源软件前五十名,让 ...
- python-unittest单元测试框架
可以理解为是已经帮我们封装好的东东,可以完成执行用例\预期与实际结果的对比等. import unittest 封装好的单元测试框架,可以直接使用 编写的测试类的继承unittest.TestCase ...
- str_1.判断两个字符串每个字符出现的次数一样
1.两个字符串每个字符出现的次数一样 $str1 = "ab'c4*"; $str2 = "cb*'a4"; $ret = isBX($str1, $str2) ...
- JDK中主要的包介绍
- OpenCV——PS滤镜算法之 球面化 (凹陷效果)
// define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_H_INCLUDED #include < ...
- python之系统编程 --线程
###########使用线程完成多任务################ from threading import Thread import time #1. 如果多个线程执行的都是同一个函数的话 ...
- iOS UINavgationController、 UINavigationBar、 UINavigationItem关系分析
一般导航控制器含有4个对象,UINavigationController.UINavigationBar.UIViewController.UINavigationItem. 1:UINavigati ...
- 使用msiexec提取msi包里的文件
核心:如需把d盘下abc.msi文件解包到目录d:\abc,操作如下:打开命令提示符,输入msiexec /a "d:\abc.msi" /qb TARGETDIR="D ...
- C++ STL std::wstring_convert处理UTF8
#include <iostream> #include <string> #include <locale> #include <codecvt> # ...