### com/blog/2016/01/14/685800.html' style='color:black'>LeetCode 70 Climbing Stairs（爬楼梯）（动态规划）（*）

class="markdown_views">

## 翻译

``你正在爬一个楼梯。它需要n步才能到底顶部。每次你可以爬1步或者2两步。那么你有多少种不同的方法爬到顶部呢？``

## 原文

``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?``

## 分析

``int dp1 = 1, dp2 = 2, dpWay = 0;``

``if (n <= 1) return dp1;if (n == 2) return dp2;``

``while ((n--)-2) {}``

``dpWay = dp1 + dp2;dp1 = dp2;dp2 = dpWay;``

``int climbStairsIter(int n,  int dpWay,int dp1, int dp2) {    if (n <= 1) return dp1;    if (n == 2) return dp2;    if ((n--) - 2) {        dpWay = dp1 + dp2;        dp1 = dp2;        dp2 = dpWay;        return climbStairsIter(n, dpWay, dp1, dp2);    }    else return dpWay;}int climbStairs(int n) {    return climbStairsIter(n, 0,1,2);}``

## 代码

``class Solution {public:    int climbStairs(int n) {        int dp1 = 1, dp2 = 2, dpWay = 0;        if (n <= 1) return dp1;        if (n == 2) return dp2;        while ((n--) - 2) {            dpWay = dp1 + dp2;            dp1 = dp2;            dp2 = dpWay;        }        return dpWay;    }};``