# 2、爬楼梯 编代码

1、采用递归实现

var climbStairs = function(n) {
if(n == 1) return 1;
if(n == 2) return 2;
return climbStairs(n-1) + climbStairs(n-2);
};


2、数组方式

var climbStairs = function(n) {
let result = [1,2];
for (let i = 2; i < n; i++) {
result.push(result[i-1] + result[i-2]);
}
return result[n-1];
};


3、ES6的方式

var climbStairs = function(n) {
let a = b = 1;
for (let i = 0; i < n; i++) {
[a, b] = [b, a + b];
}
return a;
};


temp = a;
a = b;
b = temp + b;


## 方法测速：

    console.time()
var climbStairs = function (n) {
let result = [1, 2];
for (let i = 2; i < n; i++) {
result.push(result[i - 1] + result[i - 2]);
}
return result[n - 1];
};
console.log(climbStairs(500),'数组');
console.timeEnd();

console.time()
var climbStairs = function (n) {
let a = b = 1;
for (let i = 0; i < n; i++) {
[a, b] = [b, a + b];
}
return a;
};
console.log(climbStairs(500),'ES6');
console.timeEnd();


08-26
11-07 5707
11-16 827
05-15 531
10-27 204
04-26 895
10-13 2万+
01-14 902
03-07 27
09-19 3838
05-28 901
05-25 23
10-13
10-11
08-25 576
08-04 777