版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shey666/article/details/80849381
Leetcode 70
Climbing Stairs
- Problem Description:
爬楼梯:一次可以爬1-2层楼梯,求出爬到最高一层的楼梯n时需要的步数。
具体的题目信息:
https://leetcode.com/problems/climbing-stairs/description/ - Example:
- Solution:
class Solution {
public:
int climbStairs(int n) {
if (n <= 2) return n;
int p = 1, q = 2;
for (int i = 2; i < n; i++) {
int t = p+q;
p = q;
q = t;
}
return q;
}
};