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?

Note: Given n will be a positive integer.

Example 1:

Input: 2
Output: 2
Explanation: There are two ways to climb to the top.
1. 1 step + 1 step
2. 2 steps

Example 2:

Input: 3
Output: 3
Explanation: There are three ways to climb to the top.
1. 1 step + 1 step + 1 step
2. 1 step + 2 steps
3. 2 steps + 1 step

解法:动态规划DP(Dynamic Programming)入门题。

state: dp[i] 表示爬到第i个楼梯的所有方法的和
function: dp[i] = dp[i-1] + dp[i-2]  //因为每次走一步或者两步, 所以dp[i]的方法就是它一步前和两步前方法加和
initial: dp[0] = 0; dp[1] = 1
end : return dp[n]

Java: Method 1: Time: O(n), Space: O(n)

public int climbStairs(int n) {
int[] dp = new int[n + 1];
dp[0] = 1;
dp[1] = 1;
for (int i = 2; i <= n; i++) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp[n];
}

Java: Method 2: Time:  O(n), Space: O(1)

public int climbStairs(int n) {
if (n == 0 || n == 1 || n == 2){
return n;
}
int [] dp = new int[3];
dp[1] = 1;
dp[2] = 2;
for (int i =3; i <= n; i++) {
dp[i%3] = dp[(i-1)%3] + dp[(i-2)%3];
}
return dp[n%3];
}

Java: Method 3: Time:  O(n), Space: O(1)

public class Solution {
public int climbStairs(int n) {
int[] dp = new int[]{0,1,2};
if(n < 3) return dp[n];
for(int i = 2; i < n; i++){
dp[0] = dp[1];
dp[1] = dp[2];
dp[2] = dp[0] + dp[1];
}
return dp[2];
}
}

Java:

public class Solution {
public int climbStairs(int n) {
if (n <= 1) return 1;
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];
}
}

Python: DP

class Solution(object):
def climbStairs(self, n):
if n < 3:
return n
dp = [0] * n
dp[0] = 1
dp[1] = 2
for i in range(2, n):
dp[i] = dp[i-2] + dp[i-1] return dp[n-1]  

Python:  DP, Time: O(n) Space: O(1)

class Solution:
def climbStairs(self, n):
prev, current = 0, 1
for i in xrange(n):
prev, current = current, prev + current,
return current

Python:  Recursion,Time:  O(2^n) Space: O(n)

class Solution:
def climbStairs1(self, n):
if n == 1:
return 1
if n == 2:
return 2
return self.climbStairs(n - 1) + self.climbStairs(n - 2)

C++:

class Solution {
public:
int climbStairs(int n) {
if (n <= 1) return 1;
vector<int> dp(n);
dp[0] = 1; dp[1] = 2;
for (int i = 2; i < n; ++i) {
dp[i] = dp[i - 1] + dp[i - 2];
}
return dp.back();
}
};  

 

类似题目:

[LeetCode] 53. Maximum Subarray 最大子数组

[LeetCode] 746. Min Cost Climbing Stairs

[Airbnb] Max Sum of Non-consecutive Array Elements

 

All LeetCode Questions List 题目汇总

  

  

  

 

[LeetCode] 70. Climbing Stairs 爬楼梯的更多相关文章

  1. [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 ...

  2. LeetCode 70. Climbing Stairs爬楼梯 (C++)

    题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either cl ...

  3. [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 ...

  4. Leetcode 70. Climbing Stairs 爬楼梯 (递归,记忆化,动态规划)

    题目描述 要爬N阶楼梯,每次你可以走一阶或者两阶,问到N阶有多少种走法 测试样例 Input: 2 Output: 2 Explanation: 到第二阶有2种走法 1. 1 步 + 1 步 2. 2 ...

  5. 70. Climbing Stairs爬楼梯

    网址:https://leetcode.com/problems/climbing-stairs/ 其实就是斐波那契数列,没什么好说的. 注意使用3个变量,而不是数组,可以节约空间. class So ...

  6. Leetcode#70. Climbing Stairs(爬楼梯)

    题目描述 假设你正在爬楼梯.需要 n 阶你才能到达楼顶. 每次你可以爬 1 或 2 个台阶.你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数. 示例 1: 输入: 2 输出: 2 解 ...

  7. 42. leetcode 70. Climbing Stairs

    70. Climbing Stairs You are climbing a stair case. It takes n steps to reach to the top. Each time y ...

  8. LN : leetcode 70 Climbing Stairs

    lc 70 Climbing Stairs 70 Climbing Stairs You are climbing a stair case. It takes n steps to reach to ...

  9. leetCode 70.Climbing Stairs (爬楼梯) 解题思路和方法

    Climbing Stairs  You are climbing a stair case. It takes n steps to reach to the top. Each time you ...

随机推荐

  1. django项目基于钩子验证的注册功能

    前端html <div class="agile-row"> <h3>注册</h3> {# 注册的开始#} <div class=&quo ...

  2. Python - 2和3的区别

    编码: Python2的默认编码是ASCII码,这是导致Python2中经常遇到编码问题的主要原因之一,至于原因,在于Python这门语言出现的时候,还没有Unicode! Python3默认编码是U ...

  3. datediff(date1,date2) 函数的使用

    版权声明:本文为博主原创文章,未经博主允许不得转载. 在MySQL中可以使用DATEDIFF()函数计算两个日期之间的天数 语法: datediff(date1,date2) 注:date1和date ...

  4. HBase的二级索引

    使用HBase存储中国好声音数据的案例,业务描述如下: 为了能高效的查询到我们需要的数据,我们在RowKey的设计上下了不少功夫,因为过滤RowKey或者根据RowKey查询数据的效率是最高的,我们的 ...

  5. ReactiveX 学习笔记(30)操作符辨析

    RxJava: merge/concat/switch RxJS: merge/concat/switch/exhaust RxSwift: merge/concat/switchLatest mer ...

  6. Problem F. Wiki with String

    Problem F. Wiki with StringInput file: standard input Time limit: 1 secondOutput file: standard outp ...

  7. tensorflow2.0 学习(二)

    线性回归问题 # encoding: utf-8 import numpy as np import matplotlib.pyplot as plt data = [] for i in range ...

  8. BZOJ 3672: [Noi2014]购票 树上CDQ分治

    做这道题真的是涨姿势了,一般的CDQ分治都是在序列上进行的,这次是把CDQ分治放树上跑了~ 考虑一半的 CDQ 分治怎么进行: 递归处理左区间,处理左区间对右区间的影响,然后再递归处理右区间. 所以, ...

  9. 使用nodejs+ harbor rest api 进行容器镜像迁移

    最近因为基础设施调整,需要进行harbor 镜像仓库的迁移,主要是旧版本很老了,不想使用,直接 打算部署新的,原以为直接使用复制功能就可以,但是发现版本差异太大,直接失败,本打算使用中间 版本过度进行 ...

  10. GoCN每日新闻(2019-10-06)

    GoCN每日新闻(2019-10-06) 国庆专辑:GopherChina祝大家国庆节快乐 GoCN每日新闻(2019-10-06) 1. Go 1.14 有什么新变化 http://docs.goo ...