# LeetCode 刷题： Climbing Stairs --- 类似于Fibonacci 数列

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?

class Solution {
public:
int climbStairs(int n) {
if(n<=2)
return n;

int n1 = 1;
int n2 = 2;
int temp =3;
int t;
while(temp<=n)
{
t = n1 + n2;
n1 = n2;
n2 = t;
temp++;
}
return n2;
}
};

class Solution {
unordered_map<int, int> res = {{1,1},{2,2},{3,3}};    //必须放在climbStairs 外面，全局变量
public:
int climbStairs(int n) {
if(res.find(n) == res.end())
{
res[n] = climbStairs(n-1) + climbStairs(n-2);
}
return res[n];
}
};

• 本文已收录于以下专栏：

## 【LeetCode-面试算法经典-Java实现】【073-Climbing Stairs（爬楼梯）】

【070-Climbing Stairs（爬楼梯）】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题　　You are climbing a stair case. It ta...

## LeetCode 70 Climbing Stairs（爬楼梯）（动态规划）（*）

• NoMasp
• 2016年01月14日 07:12
• 4345

## 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 70. Climbing Stairs

You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1...

## [LeetCode]70.Climbing Stairs

【题目】 You are climbing a stair case. It takes n steps to reach to the top. Each time you can eithe...

## LeetCode------Climbing Stairs

举报原因： 您举报文章：LeetCode 刷题： Climbing Stairs --- 类似于Fibonacci 数列 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)